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에 사용 가능하다.
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 directoryhtml 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