ashes icon indicating copy to clipboard operation
ashes copied to clipboard

Question: any way to extend the library?

Open mirceaulinic opened this issue 7 years ago • 2 comments

I'm very new to this library and the first question I have in mind (you may find it trivial): is there any flexible way to extend the list of features from https://github.com/mahmoud/ashes#compatibility?

More specifically, I am thinking about a way to inject external functions. If comfortable with the context, something similar to the Jinja filters. E.g.:

context.register_feature('virtual_name', feature_function_name)

And use the feature as virtual_name inside the template.

Is this currently possible or on the roadmap?

Thanks, Mircea

mirceaulinic avatar Feb 26 '17 12:02 mirceaulinic

Hi Mircea! Glad you've made it to this corner of the Python templating world. In short, yes, there are a few ways to get extensibility out of ashes.

Ashes is a Python implementation of Dust. Dust by design allows you to pass in functions as part of the template model. This gives quite a bit of extensibility in itself, but I rarely use this functionality, as I like ashes for its simplicity and portability. Python functions are simply not renderable by JavaScript. :)

Specific to this implementation, Ashes works very hard to eliminate global state. This means that every AshesEnv has its own loaders, helpers, filters, optimizers, and pragmas. You can create your own and add them to your instance of the AshesEnv. Check the DEFAULT_HELPERS and DEFAULT_FILTERS dictionaries in ashes.py for the most commonly customized features.

Customizations are bound to arise with more involved integrations with frameworks, and a basic example of that can be seen in clastic, here.

Such a long answer for such a short library, and I'm still only scratching the surface! I think it's good, though, because I've been intending to add a section on this to the docs. Please let me know if you'd like more elaboration on any angle of this. It'll help with the docs, too :)

mahmoud avatar Feb 26 '17 17:02 mahmoud

Just a quick warning that you probably already are thinking of, but I'll state just I case... Ashes is very easy to extend as mentioned above, however you should be wary of how you extend it in the context of your rendering environment.

If your templates were created by a trusted source, you have nothing to worry about. If your templates are generated by users, you should pay extra careful attention to the integration for potential security and abuse issues.

jvanasco avatar Feb 26 '17 22:02 jvanasco