ejs icon indicating copy to clipboard operation
ejs copied to clipboard

Add dynamic include (from variable)

Open phplego opened this issue 12 years ago • 59 comments

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

phplego avatar Jan 01 '13 09:01 phplego

what file did you make this change in, i also need this functionality

johnmarty avatar May 30 '13 03:05 johnmarty

Yeah I would also appreciate this feature :+1:

pwalczyszyn avatar Oct 10 '13 10:10 pwalczyszyn

This would be incredibly useful.

drewwells avatar Oct 13 '13 20:10 drewwells

Same here

carolineBda avatar Oct 28 '13 17:10 carolineBda

+1

z5h avatar Nov 27 '13 04:11 z5h

+1

marloscarmo avatar Dec 12 '13 01:12 marloscarmo

+1, please

BlackYoup avatar Dec 12 '13 23:12 BlackYoup

+1 :)

PatrickWelborn avatar Dec 29 '13 20:12 PatrickWelborn

+1

chloe899 avatar Dec 31 '13 09:12 chloe899

+1

jfhenriques avatar Jan 14 '14 08:01 jfhenriques

+1

gaurang171 avatar Jan 17 '14 18:01 gaurang171

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 :)

jasper-lyons avatar Jan 18 '14 11:01 jasper-lyons

+1

pr-nizar avatar Mar 10 '14 01:03 pr-nizar

I'd also like to have this functionality. Pull request #156 actually breaks my code for some reason.

elyran avatar Mar 10 '14 07:03 elyran

@jasper-lyons buf is an array as defined at the top of the parse function.

cjmarkham avatar Mar 29 '14 10:03 cjmarkham

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 avatar Apr 02 '14 04:04 mscdex

@mscdex +1 Absolutely correct!

elyran avatar Apr 02 '14 04:04 elyran

@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.

jasper-lyons avatar Apr 02 '14 09:04 jasper-lyons

+1

maggiben avatar Apr 12 '14 17:04 maggiben

+1

gimenete avatar Apr 16 '14 18:04 gimenete

+1

iZhen avatar Apr 26 '14 03:04 iZhen

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);

jqsjqs avatar Apr 27 '14 08:04 jqsjqs

Using this code, where do I put the variables to user for <% include myVar %> ?

AnthonyMDev avatar May 07 '14 20:05 AnthonyMDev

+1

mhandb avatar May 19 '14 14:05 mhandb

What's the difference between <% include file %> and <% include variable %>?

dgmike avatar May 19 '14 19:05 dgmike

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; %>

<% } %>

AnthonyMDev avatar May 19 '14 19:05 AnthonyMDev

+1

blowsie avatar May 30 '14 08:05 blowsie

This also seems to be a popular question on stackoverflow.

blowsie avatar May 30 '14 08:05 blowsie

+1 --Thanks phplego and jasper-lyons

cvince avatar Jun 02 '14 01:06 cvince

+1 Any chance of having this feature added?

halcarleton avatar Jun 14 '14 21:06 halcarleton