最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

可选包括在ejs中

运维笔记admin15浏览0评论

可选包括在ejs中

可选包括在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
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论