-
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
block/mixin (again) #274
Comments
A good step toward making EJS extensible, which might give us a better answer for when people have specific functionality they think needs to be added. Would be happy to work with you for some forward progress on this. |
Great, looking forward. I will look at how to implement my "mixin", and also try to port ejs-locals to an add-on of ejs. I may have some other ideas of changes/improvements along the way. But I will open separate issues, if any of them materializes. |
One tiny thought: "mixin" has some fairly specific meaning already: https://en.wikipedia.org/wiki/Mixin Basically a bag of properties included (in JS, often copied onto) in an object, without being inherited from a parent class. I would want to see how (Worth nothing also that 'template inheritance' is a thing that exists, too, but seems very different from the 'inheritance' here where we're talking about subclassing the base Template.) |
I pushed an update version for discussion. I renamed it to "snippet". I know "block" would be best. But that is used by ejs-locals (which I want to port to a plugin too). So I don't want to call it "block". The name "template" is already used to refer to the entire template, so it would be confusing too. And otherwise I am out of ideas for a good name. The overall changes in lib/ejs.js are minimal (IMHO). One question upfront: Would you be willing to include the code (as separate js file) in the main distribution?
Otherwise people would just need to require the plugin separately, and the plugin adds itself to the ejs module. The actual changes needed in lib/ejs.js
With this the plugin can now pass in a parameter name "snippet". I also modified the That way the top level template can generate ONE function for "snippet", and attach data to that function. This means that a template defined in one file (e.g. "snips.ejs") can be used
As for the plugin itself:
Any comments? |
I added tests, and it works all as expected. I also made a 2nd version, based on the changes I made in other pull requests (and branches). This version introduces a new mode <%* for control tags.
|
OFF TOPIC I know I added a lot of pull requests (and have some more coming / see branches in my fork). And probably the expected way would have been, to first check with you, if those changes are within the plans that you have for ejs. However I wanted to first see, if my ideas work as expected (and benchmark them). So I am aware that some of them may not be welcome. In which case let me know, so I can rebase the others. (If any remain, but I hope so). Btw the https://github.com/User4martin/ejs/tree/optimize-combine-append-calls https://github.com/User4martin/ejs/tree/refactor-generate-source-full-tokens together with gives some good speed gain on execution time of the generated function. |
I added docs for plugin api and snippets Any comments would be welcome. As far as I can see the snippets are ready as they are. (docs, test, code) I am working on another plugin (replacing ejs-locals). This would bring a few minor modifications to ejs. But more later. |
I am aware of #251 and #252
Please have a look at:
https://github.com/User4martin/ejs/tree/template_subclass
This is not production ready / just proof of concept
It entirely optional:
The main change to ejs is to make the Template class available, and allow to substitute it, This will allow for any sort of plugin.
(And maybe Template.modes.* should be exported separately (or on the prototype), so subclasses do not need to copy them)
I would (for better maintenance) like to see the file (when ready) included in the distribution. And maybe plugins for other features too.
But if not then at least make plugins possible by the changes in ejs.js itself.
About the ejs-mixin
This is fully up for discussion.
About the "include": There should probably be a <% mixin(name, locals) %> call instead.
That can be done, but it would need some more changes to ejs.
The Template class would need a function, that returns a list of name/function pairs, that can be used.
and there could be an additional
prepended += ' with (argument_commands || {}) {' + '\n';
That way sub classes can add commands.
The text was updated successfully, but these errors were encountered: