Ejs引擎与HTML无法正常工作

tom*_*lex 4 ejs node.js express

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

views
|
|--header.html
|--footer.html
|
|--index.html
Run Code Online (Sandbox Code Playgroud)

我配置像

app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);  
Run Code Online (Sandbox Code Playgroud)

index通过这个渲染我的模板:

res.render('index.html', {title: 'test'});
Run Code Online (Sandbox Code Playgroud)

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

类似帖子 Node.js表达:混淆ejs模板

现有示例无法正常工作 https://github.com/visionmedia/express/tree/master/examples/ejs

hex*_*ide 6

你问的原始方法是使用partials.部分已被删除,取而代之的include是EJS 的功能.这是你如何包含一个文件:

<% include header.html %>
<% include footer.html %>
Run Code Online (Sandbox Code Playgroud)

您传递给渲染页面的任何本地人也将被传递给包含.例如:

app.js

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

的index.html

<!DOCTYPE html>
<body>
  <%= other %>
  <% include content.html %>
</body>
Run Code Online (Sandbox Code Playgroud)

content.html

<pre><%= string %></pre>
Run Code Online (Sandbox Code Playgroud)

你得到的结果是:

<!DOCTYPE html>
<body>
  value
  <pre>random_value</pre>
</body>
Run Code Online (Sandbox Code Playgroud)