Ejs engine with html not working

本小妞迷上赌 提交于 2019-12-09 07:20:11

问题


i'm using html files instead of ejs, but the express engine is ejs

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

I configured like

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

I render my index template by this:

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

But how can i include header and footer.html in index.html

Similar posts Node.js express: confuse about ejs template

Existing example which is not working https://github.com/visionmedia/express/tree/master/examples/ejs


回答1:


The original method for what you asked would be to use partials. Partials have since been removed, and replaced with the include function of EJS. This is how you would include a file:

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

Any locals you pass to the rendered page will also be passed to an include. For example:

app.js

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

index.html

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

content.html

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

The resultant HTML you would get is:

<!DOCTYPE html>
<body>
  value
  <pre>random_value</pre>
</body>


来源:https://stackoverflow.com/questions/18753620/ejs-engine-with-html-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!