Maintain order on environment.extensions #3

Open
miracle2k opened this Issue Oct 20, 2010 · 2 comments

Projects

None yet

2 participants

@miracle2k

Currently, an environment's extensions are simply stored as an unsorted dict. This is problematic if you are not fully in control of what extensions are added to the environment, and you want to (reliably) override a tag.

In my case, I am using Coffin to integrate Jinja2 with Django, which provides a replica of the "url" tag. I need to replace this default url tag with a custom version, and to do that, I currently need to manually remove the existing url extensions from the environment.extensions dictionary.

If the extensions were stored with order attached, then an extension added later could take preference over one registered earlier.

@mitsuhiko
Member

For of all, I would not use a tag for "url" but instead just put a function into the context. Reasoning: can be used in expressions which makes i18n support easier etc.

Secondly I am for improving the extension mechanism there, but I am not sure yet how that is supposed to look like. Ideally there would be a way for a template to dynamically load an extension if necessary, but the current extension interface is not flexible enough for that. This would requite some refactoring of the Jinja2 internals but it might be worth it.

Not convinced that changing the extensions dict into an ordered directory is a good idea. You actually can already provide a priority for extensions, not sure if that is taken into account for registering tags though. In case it does not, supporting that for tags might be a quickfix.

@miracle2k

You actually can already provide a priority for extensions, not sure if that is taken into
account for registering tags though. In case it does not, supporting that for tags might
be a quickfix.

I wasn't aware that there there were priorities, or I forgot about it.

It appears that the priority does affect tags - fwiw, the test case I used to try this is here:

http://github.com/miracle2k/jinja2/commit/c7ca7fc1698f8a9be3447e9b137da73381f9e836

Basically, that's good enough for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment