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

Allow env_options in `Jinja2Templates #1401

Merged
merged 6 commits into from
Jan 19, 2022

Conversation

aminalaee
Copy link
Member

@aminalaee aminalaee commented Jan 9, 2022

Closes #427

Allows Jinja2Templates to set Jinja2 Environment options here.

For example:

templates = Jinja2Templates(
        directory=directory, autoescape=False, auto_reload=True, enable_async=True
)

Or:

loader = jinja2.FileSystemLoader(directory)

templates = Jinja2Templates(
        directory=directory, loader=loader
)

Since directory is still a required argument, it will be passed in this case, but the loader passed will take precedence over the directory and the loader created by Starlette. I can't think of any way to handle this right now, until maybe we can consider another API for Jinja2Templates later. This way we can deal with issues like #1214 .

  • Tests
  • Docs

) -> "jinja2.Environment":
@pass_context
def url_for(context: dict, name: str, **path_params: typing.Any) -> str:
request = context["request"]
return request.url_for(name, **path_params)

loader = jinja2.FileSystemLoader(directory)
env = jinja2.Environment(loader=loader, autoescape=True)
env_options.setdefault("loader", loader)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can decide if we want to allow loader to be passed in env_options or not. The point is if we add it it makes directory reduntant but allows user full control over the loader.

@aminalaee aminalaee requested a review from a team January 16, 2022 14:24
@aminalaee aminalaee changed the title Allow in env_options in `Jinja2Templates Allow env_options in `Jinja2Templates Jan 17, 2022
Copy link
Member

@tomchristie tomchristie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat. 👍

@aminalaee
Copy link
Member Author

The documentation was not done yet. Can you check again?

@tomchristie
Copy link
Member

Yup - I oughta have noted that. 😅
Docs look good.

@aminalaee aminalaee mentioned this pull request Jan 19, 2022
8 tasks
@aminalaee aminalaee merged commit 3e6b26d into master Jan 19, 2022
@aminalaee aminalaee deleted the allow-env-options-in-templating branch January 19, 2022 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

templateing: jinja2: pass kwargs for environment
2 participants