将变量传递给EJS include
我在几个地方使用了一个全局标头,我试图在呈现模板时可以通过的变量中定义其位置。
I have a global header used in a couple of places and I was trying to define its location in a variable that could be passed when rendering a template.
有什么地方像这样:
var headerLocation = 'some/location/header.ejs';
res.render( viewDir + '/index', {
header: headerLocation
} );
在模板文件中:
<% include header %>
标题是与渲染一起传递的值。
header being the value passed in with the render.
似乎不可能,但也许我错过了一些东西,以为我想在这里问。
It doesn't seem to be possible but maybe I missed something so thought I'd ask here.
编辑:
下面的答案注释中提到了此问题,但总结起来,现在EJS版本2中可用。
This is mentioned in comments on answers below but to summarize, this is now available in version 2 of EJS.
请参见此处: https://github.com/mde/ejs#includes
以及此处的相关讨论:https://github.com/tj/ejs/issues/93
已添加此功能:如果它不是路径(找不到文件),则将其评估为变量名。 https://github.com/visionmedia/ejs/pull/156
This feature has been added: if it is not path (file not found), it is evaluated as variable name. https://github.com/visionmedia/ejs/pull/156