如何在EJS版本2中动态包含带变量的文件?

时间:2016-08-29 07:37:34

标签: node.js sails.js ejs

最近,我正在使用EJS(版本 - 0.8.4),该文件在File:node_modules / lib / ejs.js中的更新方式正常工作在第155行:

旧代码:

 if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
if (!filename) throw new Error('filename option is required for includes');
var path = resolveInclude(name, filename);
include = read(path, 'utf8');
include = exports.parse(include, { filename: path, _with: false, open: open, close: close, compileDebug: compileDebug });
buf.push("' + (function(){" + include + "})() + '");
js = '';  }

到此代码:

if (0 == js.trim().indexOf('include')) {
var name = js.trim().slice(7).trim();
if (!filename) throw new Error('filename option is required for includes');
// If it is not path, but variable name (Added)
if(options[name])
     var path = resolveInclude(options[name], filename);
else
     var path = resolveInclude(name, filename);
include = read(path, 'utf8');
include = exports.parse(include, options); // Added transfer whole options
buf.push("' + (function(){" + include + "})() + '");
js = '';  }

在ejs文件中简单写:

<%- include variable%> in place of <%- include header.ejs %>

其中varibale = header或其他包含filepath。

但它没有与EJS合作(版本:2.3.4),我该怎么做才能解决这个问题,因为如果它无法解决,我将遇到大麻烦。

还有一件事要问:在这个更新版本(2.3.4)中是否有替代ejs.filter?

1 个答案:

答案 0 :(得分:0)

而不是使用<%- include variable%> 尝试使用&lt;% - include(variable)%&gt;它将内部值视为变量。

如果你跳过括号,它将解释为字符串“variable”并搜索该ejs文件。