包含带有语句的 ejs

include ejs with statement

我想知道是否可以仅在用户登录时将我的 header 包含在 ejs 中。 我试过这个:

<% if(connect) { %>
    <%- include('../partials/header-login'); %>
<%  } else { %>
    <%- include('../partials/header'); %>
<% } %>

有人有解决方案吗?

你走对了! 来自文档:

Html 模板:

    <% if (user) { %>   <h2><%= user.name %></h2> <% } %>

用法:

let template = ejs.compile(str, options);
template(data);
// => Rendered HTML string

ejs.render(str, data, options);
// => Rendered HTML string

ejs.renderFile(filename, data, options, function(err, str){
    // str => Rendered HTML string
});

您可以在官方文档中查看更多详细信息:https://ejs.co/#docs

为了更好地使用Whosebug,您可以参考本指南:https://whosebug.com/help/how-to-ask