Template engine?
HTML의 정적인 단점을 개선
반복문, 조건문, 변수 등을 사용할 수 있음
동적인 페이지 작성 가능
const pug = require('pug')
app.set('views', //템플릿이 저장되는 디렉토리가 어딘지 지정
path.join(__dirname,'views'))
app.set('view engin',pug); //퍼그라는 템플릿을 사용할 것이라고 선언
#login-button >> <div id="login-button"></div>
.post-image >> <div class="post-image"></div>
span#highlight >> <span id="highlight"></span>
button(type="submit") 전송 >> <button type="submit">전송</button>
script.
const message = 'Pug';
alert(message);
===
<script>
const message='Pug'
alert(message);
</script>
router.get('/',function(req,res,next)=>{
res.render('index',{title:'Express'}); // title이란 변수에 express 할당
});
router.get('/',function(req,res,next){
res.locals.title='Express'; // title이란 변수에 express 할당
res.render('index');
})
h1 = title
p Welcome to #{title}
button(class=title,type='submit') 전송
input(placeholder=title+'연습')
- const node = 'node'
- const js = 'javascript'
p #{node}와 #{js} >> <p>node와 javascript</p>
each fruit in ['사과','배','복숭아']
li=fruit
if isLoggedIn
div 로그인 되었습니다
else
div 로그인이 필요합니다.
case fruit
when 'apple'
p 사과입니다.
when 'banana'
p 바나나입니다
default
p 과일이 아닙니다.
---layout.pug---
html
head
title=title
body
block content
---main.pug---
extends layout
block content
h1 Main Page
---listImem.pug---
mixin listItem(title,name)
tr
td title
td name
--- main.pug---
include listItem
table
tbody
listItem('제목','이름')
const nunjucks = require('nunjucks');
nunjucks.configure('views', { express: app, watch: true, });
<h1>{{title}}</h1>
<p>welcome to {{title}}</p>
<button class="{{title}}" type="submit">전송</button>
{% set node ='Node.js'%}
{% set js ='javascript'%}
<p>{{node}}와{{js}}</p> >> <p>Node.js와 javascript</p>
<p>{{'<strong>이스케이프</strong>'}}</p>
<p>{{'<strong>이스케이프 안함</strong>'|safe}}</p>
<ul>
{% set fruits =['사과','배','오렌지'] %} {% for item in fruits %}
<li>{{item}}</li>
{% endfor %}
</ul>
<ul>
{% set fruits =['사과','배','오렌지'] %} {% for item in fruits %}
<li>{{loop.index}}번째 {{item}}</li>
{% endfor %}
</ul>
{% if isLoggedIn %}
<div>로그인 되었습니다.</div>
{% else %}
<div>로그인이 필요합니다.</div>
{% endif %}
{% if fruit ==='apple' %}
<div>사과입니다.</div>
{% elif ==='바나나' %}
<div>바나나입니다.</div>
{% else %}
<p>과일이 아닙니다.</p>
{% endif %}
<!-- header.html -->
<header>
<a href="/">home</a>
<a href="/about">about</a>
</header>
<!-- footer.html -->
<footer>
<div>푸터입니다.</div>
</footer>
<!-- main.html -->
{% include "header.html" %}
<main>
<h1>메인</h1>
<p>문장</p>
</main>
{% include "footer.html" %}
<!-- layout.html -->
<html>
<header>
<title>{{title}}</title>
<link rel="stylesheet" href="" />
</header>
<body>
<header>{% block content %} {% endblock %}</header>
<footer>{% block script %} {% endblock %}</footer>
</body>
</html>
<!-- body.html -->
{% extends 'layout.html' %} \
<!-- -->
{% block content %}
<main>
<p>내용</p>
</main>
{% endblock %} {% block script %}
<script src="/main.js"></script>
{% endblock %}