4

我使用的是 html 文件而不是 ejs,但是 express 引擎是 ejs

views
|
|--header.html
|--footer.html
|
|--index.html

我像这样配置

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);  

我通过以下方式渲染我的index模板:

res.render('index.html', {title: 'test'});

但是我如何在 index.html 中包含 header 和 footer.html

类似帖子 Node.js express: confuse about ejs template

现有示例不起作用 https://github.com/visionmedia/express/tree/master/examples/ejs

4

1 回答 1

6

您所要求的原始方法是使用部分。部分已被删除,并替换include为 EJS 的功能。这是包含文件的方式:

<% include header.html %>
<% include footer.html %>

您传递给渲染页面的任何本地变量也将传递给包含。例如:

应用程序.js

app.get('/', function(req, res) {
  res.render(__dirname + '/index.html', {
    string: 'random_value',
    other: 'value'
  });
});

索引.html

<!DOCTYPE html>
<body>
  <%= other %>
  <% include content.html %>
</body>

内容.html

<pre><%= string %></pre>

您将得到的结果 HTML 是:

<!DOCTYPE html>
<body>
  value
  <pre>random_value</pre>
</body>
于 2013-09-12T00:52:32.863 回答