-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dynamic include (from variable) #93
Comments
what file did you make this change in, i also need this functionality |
Yeah I would also appreciate this feature 👍 |
This would be incredibly useful. |
Same here |
+1 |
1 similar comment
+1 |
+1, please |
+1 :) |
+1 |
2 similar comments
+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: 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 |
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 |
2 similar comments
+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. :)
|
Using this code, where do I put the variables to user for <% include myVar %> ? |
+1 |
What's the difference between |
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++ ) {
<% include fileName %>
<% } %> |
+1 |
This also seems to be a popular question on stackoverflow. |
+1 --Thanks phplego and jasper-lyons |
+1 |
+1 Please merge |
Any update please? I am also looking for the same solution. |
Thank you jasper-lyons and phplego! |
👍 Please merge - i would totally reduce my code! |
Not working for me. |
This is waiting for more than a year now, it would be great to have such basic feature! |
Try Jade, it supports includes AND inheritance through blocks. I've moved over. |
If you use this solution, think to disable the app.set('view cache', false); |
I cloned this fork. Still i am unable to make it work. it is said here it is work like <% include var_name %> So i tried to use it this way <% include mytemp %> It thorws error E:\node-examples\irobots\nodecontrolbot\express-dashboard\views\mytemp .ejs not found. Is there spefic any to declare this? |
I am waiting for more than a year this feature is ready??? |
+1 very usefull funtionnality, i need it now, it will be nice if we can pass the name of the template as a variable and also some data. |
+1 would be great |
To note, I've added a simple test in the test cases for this and removed a jshint error occurring. Does anyone have any further ideas on how this could be implemented safer / more semantically? For example, with the implementation in my fork, a template including it's self will cause a stack overflow. How would this situation be managed? Remove variables used to resolve templates from the scope of the included templates? Prevent a template from including it's self? In my experience with templating systems, small as it is, people seem to end up wanting templates to be as composable as functions though i believe that would require some serious changes. |
+1 |
pretty please |
+1 |
+1 |
1 similar comment
+1 |
Note that v2 of EJS implements |
Many thanks @mde - This is exactly what I was looking for. |
wow, it's work 100%, good job. |
This issue exists no more, one can use include as function just like @mde said (thanks by the way). So I think it would be good to close it, don't you think so @tj? For anyone else who reaches this page, this is how it works:
Where someVar and someObj is defined, e.g., like this:
With these lines in you app, you would render template.ejs file and include partials/index.ejs on it. Oh, and partials/index.ejs would receive someObj variable to use it internally. |
In fact, this entire repo is essentially closed. It's only here for historical reference. |
Oh, I see, sorry. |
If you want dynamic variable in ejs include statement ,just try this |
http://ejs.co/
server.js |--views |
@edilsonvilarinho I have encountered the same issue with "ejs": "^2.5.7", "express": "^4.16.2". Did you find any solution? |
@smndey `var express = require('express'); var app = express(); app.set( 'view engine' , 'ejs' ); app.use(express.static(process.cwd() + '/app/public')); consign() module.exports = app;` |
For support <% include var_name %>
Please change this code (file ejs.js at line ~ 155):
To this code:
Эти два небольших исправления добавляют возможность использовать переменные в теге
<% include var_name %>
EDIT: File: node_modules/lib/ejs.js at line ~ 155
The text was updated successfully, but these errors were encountered: