使用 Express 模板引擎
¥Using template engines with Express
模板引擎使你能够在应用中使用静态模板文件。在运行时,模板引擎将模板文件中的变量替换为实际值,并将模板转换为发送给客户端的 HTML 文件。这种方法使设计 HTML 页面变得更加容易。
¥A template engine enables you to use static template files in your application. At runtime, the template engine replaces variables in a template file with actual values, and transforms the template into an HTML file sent to the client. This approach makes it easier to design an HTML page.
Express 应用生成器 使用 Pug 作为其默认值,但它还支持 Handlebars 和 EJS 等。
¥The Express application generator uses Pug as its default, but it also supports Handlebars, and EJS, among others.
要渲染模板文件,请在生成器创建的默认 app.js
中设置以下 应用设置属性:
¥To render template files, set the following application setting properties, in the default app.js
created by the generator:
-
views
,模板文件所在目录。例如:app.set('views', './views')
。这默认为应用根目录中的views
目录。¥
views
, the directory where the template files are located. Eg:app.set('views', './views')
. This defaults to theviews
directory in the application root directory. -
view engine
,使用的模板引擎。例如,要使用 Pug 模板引擎:app.set('view engine', 'pug')
。¥
view engine
, the template engine to use. For example, to use the Pug template engine:app.set('view engine', 'pug')
.
然后安装对应的模板引擎 npm 包;例如安装 Pug:
¥Then install the corresponding template engine npm package; for example to install Pug:
$ npm install pug --save
兼容 Express 的模板引擎(例如 Pug)导出一个名为 __express(filePath, options, callback)
的函数,res.render()
调用该函数来渲染模板代码。
¥Express-compliant template engines such as Pug export a function named __express(filePath, options, callback)
,
which res.render()
calls to render the template code.
一些模板引擎不遵循这个约定。@ladjs/consolidate 库通过映射所有流行的 Node.js 模板引擎来遵循这个约定,因此可以在 Express 中无缝工作。
¥Some template engines do not follow this convention. The @ladjs/consolidate library follows this convention by mapping all of the popular Node.js template engines, and therefore works seamlessly within Express.
设置好视图引擎后,你的应用中不需要指定引擎,也不需要加载模板引擎模块;Express 在内部加载模块,例如:
¥After the view engine is set, you don’t have to specify the engine or load the template engine module in your app; Express loads the module internally, for example:
app.set('view engine', 'pug')
然后,在 views
目录中创建一个名为 index.pug
的 Pug 模板文件,内容如下:
¥Then, create a Pug template file named index.pug
in the views
directory, with the following content:
html
head
title= title
body
h1= message
创建路由以渲染 index.pug
文件。如果未设置 view engine
属性,则必须指定 view
文件的扩展名。否则,你可以省略它。
¥Create a route to render the index.pug
file. If the view engine
property is not set,
you must specify the extension of the view
file. Otherwise, you can omit it.
app.get('/', (req, res) => {
res.render('index', { title: 'Hey', message: 'Hello there!' })
})
当你向主页发出请求时,index.pug
文件将渲染为 HTML。
¥When you make a request to the home page, the index.pug
file will be rendered as HTML.
视图引擎缓存不缓存模板输出的内容,只缓存底层模板本身。即使缓存打开,视图仍会随每个请求重新渲染。
¥The view engine cache does not cache the contents of the template’s output, only the underlying template itself. The view is still re-rendered with every request even when the cache is on.