Skip to content
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

Configuring Jinja2 extensions #117

Closed
miracle2k opened this issue Apr 16, 2015 · 6 comments
Closed

Configuring Jinja2 extensions #117

miracle2k opened this issue Apr 16, 2015 · 6 comments

Comments

@miracle2k
Copy link

Jinja2 has this idea that when extensions need configuration, it should be done by setting attributes on the environment. It seems to be that this is currently pretty hard - I am doing this now:

from jinja2 import Environment as Jinja2Environment
class ConfiguredJinja2Environment(Jinja2Environment):
    def __init__(self, *a, **kw):
        Jinja2Environment.__init__(self, *a, **kw)
        from django_assets.env import get_env
        self.assets_environment =   get_env()

TEMPLATES = [
{
    "BACKEND": "django_jinja.backend.Jinja2",
    "OPTIONS": {
        "environment": 'myproject.settings.ConfiguredJinja2Environment',
@niwinz niwinz added the Task label Apr 21, 2015
@niwinz niwinz self-assigned this Apr 21, 2015
@niwinz
Copy link
Owner

niwinz commented Apr 21, 2015

I'm not pretty sure that this approach is really good idea, independently of how jinja2 convention is :S

In my opinion, the extesion should be isolated and self-sufficient.

Use environment for set some params that some extensions may use, seems like a very bas approach. I'm missing something?

@miracle2k
Copy link
Author

http://jinja.pocoo.org/docs/dev/extensions/#jinja2.ext.Extension

As extensions are created by the environment they cannot accept any arguments for configuration. One may want to work around that by using a factory function, but that is not possible as extensions are identified by their import name. The correct way to configure the extension is storing the configuration values on the environment.

So there really isn't any other way, and some extensions will need configuration. See also for example the fragment extension example in the Jinja2 docs.

@niwinz
Copy link
Owner

niwinz commented Apr 21, 2015

I understand... I will think something for solve it.

@niwinz
Copy link
Owner

niwinz commented Apr 29, 2015

I think that an other solution for this is some kind of hook, that will be called when all environment is up and running, that hook will receive the environment. This will decouple a little bit the environent from the configurations of third party extensions.

That do you think?

@miracle2k
Copy link
Author

I like the idea. I don't necessarily buy the decoupling argument, but since Jinja doesn't have an API for this and you are essentially just making arbitrary modifications of the environment object, it seems just as well that the user should do this in code, not via some interface.

@wizpig64
Copy link
Collaborator

wizpig64 commented Mar 7, 2022

closing this as it's 7 years old.

@wizpig64 wizpig64 closed this as completed Mar 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants