可选包括在ejs中
如果只存在,我可以选择包含部分吗?
例如,
<%- include('some-template'); %>
应遵守:
如果some-template.ejs
存在:
// content of some-template.ejs
如果some-template.ejs
不存在:
// nothing
目前,如果部分不存在,则会抛出错误,例如:
ENOENT: no such file or directory, open 'some-template'
回答如下:
只需用if检查是否存在文件:
<% if (fs.existsSync('views/some-template.ejs')) { %>
<%- include('some-template'); %>
<% } %>
确保“views”文件夹与条件内的路径匹配。
此外,您必须将fs对象发送到引擎,以便可以在父模板的scriptlet中使用它:
res.render("main-template", {
fs: fs
});
可选包括在ejs中
如果只存在,我可以选择包含部分吗?
例如,
<%- include('some-template'); %>
应遵守:
如果some-template.ejs
存在:
// content of some-template.ejs
如果some-template.ejs
不存在:
// nothing
目前,如果部分不存在,则会抛出错误,例如:
ENOENT: no such file or directory, open 'some-template'
回答如下:
只需用if检查是否存在文件:
<% if (fs.existsSync('views/some-template.ejs')) { %>
<%- include('some-template'); %>
<% } %>
确保“views”文件夹与条件内的路径匹配。
此外,您必须将fs对象发送到引擎,以便可以在父模板的scriptlet中使用它:
res.render("main-template", {
fs: fs
});