问题
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