-
Notifications
You must be signed in to change notification settings - Fork 847
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
"Require" Pattern for Actual Reusable Dynamic Functions Alongside Traditional Includes #328
Comments
This seems similar to snippets https://github.com/User4martin/ejs/blob/plugin-opts-filter/docs/plugin-snippet.md If you want to return data from an include you do not need require. You can add then to an object on the locals variable.
foo can add keys to locals.utils. foo is given a flat copy of locals. Therefore the caller will not see changes made to locals directly. However locals.utils is a reference, and foo gets the same reference, effectively allowing to add/modify keys in utils and those changes being visible to the caller. |
Nope, that doesn't work since every include gets its own '__output' array which means when you actually call functions declared inside the include, everything gets appended to the wrong array, meaning you can't use any html and ejs-tags inside them, defeating the whole purpose of ejs. I don't want to return data, but reusable real functions that can utilize ejs tags. Also, that snippets thing you linked to: That suffers from some of the same problems I've listed above. Why use something more limited and needlessly complex, when you could just as easily use functions? |
Some thoughts here: There are lots of different ways to think about code reuse in a templating system. You are arguing for a functional approach. Some people advocate for template inheritance, which brings in a class-based approach. I prefer to think of this in terms of content reuse, and don't think we should need a lot of programming modularity inside of our templates. (I agree with the Mustache folks on this -- I just think inventing yet another programming language to do it is very silly.) For a templating system, the ability to include recursively the content of one rendered template inside of another is sufficient. (I would also argue that parameters should not be automatically declared. Being more explicit is generally good when you're moving between contexts (from your top-level handler function, into the template, for instance). However, if you really dig the functional approach -- what you're describing here actually sounds a lot like tagged template literals (see midway down the page here): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals And if you're intent on using EJS syntax rather than simple interpolation, you could easily wrap an EJS This is not something we'd implement in EJS core. I'm going to close this issue, but happy to reopen if further discussion is warranted. |
I feel like you're more arguing about philosophy and the fact that I called it "require-style-includes" instead of "requiring files" or something here. Look, isn't one of ejs main selling points that it's just Javascript and that it just works like you'd expect? That just isn't the case when it comes to includes and functions. And seeing as ejs is just Javascript: I'm not introducing some completely crazy new ideas here either, in fact, what I'm using here, is one of the very basic building blocks of Javascript and something everyone should be familiar with when working with node. If you look at some of the other issues around here you can clearly see that "blocks," "snippets" or whatever you want to call them are something that ejs lacks and I just don't see why you have to invent a complex new system, when actual functions could do the same job and probably better. At the end of the day, do you really want to reinvent functions (but likely not as good), just so you don't have to call them functions? Also, in my opinion, that parameters are not automatically declared can be a big problem, because it means that you have to be always aware of the contents of your 'locals'-object to not leak in values, which means "being explicit" isn't even optional anymore. Lastly, I don't know if you've seen my pull request (#329), but it's literally just a 20-line change, that doesn't impact any other aspect of ejs and doesn't break older code either. Both "philosophies" that include() and require() represent can happily co-exist without any problems and I feel like in this case having more options at your disposal doesn't have any downsides at all. |
@mde Yes, I would like to discuss this further. See above. |
@mde I'm still waiting on more input. |
When it comes to dynamic reusable snippets, I feel like the current include system is a bit lacking:
I think the 'require'-pattern would be much better for this:
My solution would be adding a require() function to the local scope, that works similar to include(), but returns an object or function instead of a compiled string.
This wouldn't break anything, since both include and require can exist without problems.
On top of that, it isn't actually that difficult to implement, as it's just a matter of passing down the '__output' array and returning an exports object instead of the joined array.
In fact, I've already successfully implemented it in a local copy and I just wanted to ask here first before I create a pull request.
The text was updated successfully, but these errors were encountered: