问题
通过一些在线示例,您可以使用这样的语法
<%- include hello-world %>
Run Code Online (Sandbox Code Playgroud)
或者甚至你可以使用
<%- include('hello-world'); %>
Run Code Online (Sandbox Code Playgroud)
您可能会收到包含缺少文件名的错误
Exception occurred: Error: `include` requires the 'filename' option.
Run Code Online (Sandbox Code Playgroud)
问:如果我的语法正确,问题出在哪里?
答案在后台“文件路径”
但即使你使用了正确的路径,你也会得到一个错误
var fs = require('fs');
ejs.render(fs.readFileSync(__dirname + '/templates/include.ejs', 'utf8'), {});
Run Code Online (Sandbox Code Playgroud)
正确答案是“使用 renderFile ”
ejs.renderFile(__dirname + '/templates/include.ejs', {}, function(err, result) {
if (!err) {
res.end(result);
}
else {
res.end(err.toString());
console.log(err);
}
});
Run Code Online (Sandbox Code Playgroud)
参考
| 归档时间: |
|
| 查看次数: |
3732 次 |
| 最近记录: |