ejs
ejs copied to clipboard
Add dynamic include (from variable)
For support <% include var_name %> Please change this code (file ejs.js at line ~ 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 = '';
}
To this code:
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 = '';
}
Эти два небольших исправления добавляют возможность использовать переменные в теге <% include var_name %>
EDIT: File: node_modules/lib/ejs.js at line ~ 155
what file did you make this change in, i also need this functionality
Yeah I would also appreciate this feature :+1:
This would be incredibly useful.
Same here
+1
+1
+1, please
+1 :)
+1
+1
+1
This code is wrong btw.
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 = '';
}
In this line,
buf.push("' + (function(){" + include + "})() + '");
buf is a string which does not have a push method. It should be changed to this to work
buf. += "' + (function(){" + include + "})() + '";
For those wondering where to add this code: node.js - it should be added to node_modules/lib/ejs.js at line:155 in browser - it should be added to the ejs.js file before minification at line:207
I changed the code and made a pull request :)
+1
I'd also like to have this functionality. Pull request #156 actually breaks my code for some reason.
@jasper-lyons buf
is an array as defined at the top of the parse
function.
I'm not too thrilled about the magic in #156, I'd rather see a separate token (e.g. 'include_dyn') for this behavior to make it very clear what is expected.
@visionmedia Any chance of including this kind of behavior in some way? I dislike maintaining forks for small changes like this and this particular feature seems to be desired by quite a few people.
@mscdex +1 Absolutely correct!
@cjmarkham In the code base I made the fork from buf is most certainly a string, please feel free to have a look.
@mscdex On reflection I agree, I had a need for the functionality and it seemed a lot of others did too so I merely adapted @phplego 's code.
+1
+1
+1
Hi... the original solution works quite well for a single property of the "options" object (i.e., object.property1 is resolvable via options["property1"]). This doesn't work quite so well if I include an object in options that itself has properties. The use case here is that I have 5 or 6 templates I'm using to construct a page and rather include a var for each path at the root of the "options" object, I'd rather be able to specify the property of an object within "options" (i.e., <% include myObj.headers %> ).
I'm in a bit of a hurry tonight, so this won't be the most elegant code, but it does work and covers the original use case. Note that it does split and evaluate literal filenames to determine if they're in the options obj... so if you happen to create an object within "options" like this: options.filename-stem.ejs then whatever value is in there will take precedence. :)
var resolvedObj = null;
var objName = name.split(".");
if (objName) {
resolvedObj = options;
for (var idx=0; idx<objName.length; idx++) {
resolvedObj = resolvedObj[objName[idx]];
if ( ! resolvedObj) {break;}
}
}
if(resolvedObj)
var path = resolveInclude(resolvedObj, filename);
else
var path = resolveInclude(name, filename);
Using this code, where do I put the variables to user for <% include myVar %> ?
+1
What's the difference between <% include file %>
and <% include variable %>
?
I have a system where users can create dynamic content using a WYSIWYG editor, which is saved in an HTML file. I want to be able to display a list of all of the user generated content at runtime without knowing the files at compile time.
For instance, I'd be able to like to write code in ejs like this:
<% for (i = 0; i <= listOfFiles.length; i++ ) { file = listOfFiles[i].name; %>
<% } %>
+1
This also seems to be a popular question on stackoverflow.
+1 --Thanks phplego and jasper-lyons
+1 Any chance of having this feature added?