Template Engines: Pug and Handlebars
一个模版引擎是一个库或框架。它用一些rules/languages来解释data和渲染views。
web app中,views可能是HTML pages(或部分它们),但是也可以是JSON, XML文件, 或者在desktop programs中是GUIs。
MVC框架,templates属于view。
在web app内,使用模版可以动态地生成无限多个pages。
之后讲如何使用Pug和Handlebars
然后是:
Project: Adding Pug Templates to Blog
-
layout.pug
: Global app-wide template -
index.pug
: Home page with the list of posts -
article.pug
: Individual article page -
login.pug
: Page with a login form -
post.pug
: Page for adding a new article -
admin.pug
: Page to administer articles after logging in
Because the templates in this mini-project require data, we'll skip the demo until Chapter 5, where we'll plug in the MongoDB database. So the source code for the Pug templates is exactly the same as in the code/ch5
folder of the GitHub repository azat-co/practicalnode
: https://github.com/azat-co/practicalnode. Feel free to copy it from there or follow the instructions to implement listed below in this section.