A Template Engine: Pug

서정준·2022년 6월 29일
0

NPM

목록 보기
4/4
post-thumbnail
post-custom-banner

우리가 template engine을 사용해야 하는 이유

1. A template engine enables you to use static template files in your application.
→ web application은 components(head, body, footer, etc)들의 집합인데 각각의 component를 재사용 할 수 있다.
2. At runtime, the template engine replaces variables in a template file with actual values
→ server측의 데이터(변수)를 받아 template에 사용 가능하다.


Template(Pug)를 사용하는 방법은?

Step 1: Installing express and the pug template engine

	npm install express pug

Step 2: Setting up the view engine

    const express = require("express")
    const app = express();
    // Set the View Engine or Template Engine
    // views: the directory where the template files are located.
    // View Engine: the template engine to use
    app.set('views', './views');, '
    app.set('view engine', 'pug');
    app.listen(3000)

Step 3: Setting up the view engine
Create a Pug template file named index.pug in the views directory

html
  head
    title= title
  body
    h1= message

step 4: Then create a route to render the index.pug file.

app.get('/', (req, res) => {
  res.render('index', { title: 'Hey', message: 'Hello there!' })
})

Step 5: When you make a request to the home page, the index.pug file will be rendered as HTML.


자료 출처

https://expressjs.com/en/guide/using-template-engines.html
https://www.educative.io/answers/what-are-template-engines

profile
통통통통
post-custom-banner

0개의 댓글