使用 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 一起使用的一些流行的模板引擎是 PugMustacheEJSExpress 应用生成器 使用 Jade 作为默认值,但它也支持其他几种。

¥Some popular template engines that work with Express are Pug, Mustache, and EJS. The Express application generator uses Jade as its default, but it also supports several others.

请参阅 模板引擎 (Express wiki) 以获取可与 Express 一起使用的模板引擎列表。另见 比较 JavaScript 模板引擎:Jade、Mustache、Dust 等

¥See Template Engines (Express wiki) for a list of template engines you can use with Express. See also Comparing JavaScript Templating Engines: Jade, Mustache, Dust and More.

注意:Jade 已更名为 Pug。你可以继续在你的应用中使用 Jade,它会正常工作。但是,如果你想要模板引擎的最新更新,则必须在应用中将 Jade 替换为 Pug。

¥Note: Jade has been renamed to Pug. You can continue to use Jade in your app, and it will work just fine. However if you want the latest updates to the template engine, you must replace Jade with Pug in your app.

要渲染模板文件,请设置以下 应用设置属性,在生成器创建的默认应用中的 app.js 中设置:

¥To render template files, set the following application setting properties, set in app.js in the default app created by the generator:

然后安装对应的模板引擎 npm 包;例如安装 Pug:

¥Then install the corresponding template engine npm package; for example to install Pug:

$ npm install pug --save

符合 Express 标准的模板引擎(例如 Jade 和 Pug)导出一个名为 __express(filePath, options, callback) 的函数,res.render() 函数调用该函数来渲染模板代码。

¥Express-compliant template engines such as Jade and Pug export a function named __express(filePath, options, callback), which is called by the res.render() function to render the template code.

一些模板引擎不遵循这个约定。Consolidate.js 库通过映射所有流行的 Node.js 模板引擎来遵循这个约定,因此可以在 Express 中无缝工作。

¥Some template engines do not follow this convention. The Consolidate.js 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, as shown below (for the above example).

app.set('view engine', 'pug')

views 目录下创建一个名为 index.pug 的 Pug 模板文件,内容如下:

¥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 文件的扩展名。否则,你可以省略它。

¥Then 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.

注意:视图引擎缓存不缓存模板输出的内容,只缓存底层模板本身。即使缓存打开,视图仍会随每个请求重新渲染。

¥Note: 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.

要了解有关模板引擎如何在 Express 中工作的更多信息,请参阅:“为 Express 开发模板引擎”

¥To learn more about how template engines work in Express, see: “Developing template engines for Express”.