带有HTML的Ejs引擎不起作用 [英] Ejs engine with html not working
问题描述
我使用的是HTML文件而不是ejs,但快递引擎为ejs
i'm using html files instead of ejs, but the express engine is ejs
views
|
|--header.html
|--footer.html
|
|--index.html
我配置为
app.set('views', __dirname + '/views');
app.engine('html', require('ejs').renderFile);
我通过以下方式渲染 index
模板:
I render my index
template by this:
res.render('index.html', {title: 'test'});
但是我如何在header.html中包含header和footer.html
But how can i include header and footer.html in index.html
类似的帖子
Node.js表达:混淆ejs模板
现有示例无法正常运行
https://github.com/visionmedia/express/tree/master/examples/ejs
Existing example which is not working https://github.com/visionmedia/express/tree/master/examples/ejs
推荐答案
您要求的原始方法是使用局部函数。此后已删除部分函数,并用EJS的 include
函数代替。这就是包含文件的方式:
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>
您将获得的HTML结果是:
The resultant HTML you would get is:
<!DOCTYPE html>
<body>
value
<pre>random_value</pre>
</body>
这篇关于带有HTML的Ejs引擎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!