-
-
Notifications
You must be signed in to change notification settings - Fork 933
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
Add env argument to Jinja2Templates, remove **env_options. (ref #2134) #2159
Conversation
I like this, it meets the "starlette has a simple opinionated way" need whilst also allowing the user to do something really complicated without the starlette project having to think about a new API at all - since it falls to a shared idea of the Jinja environment which is referenced and used in many different jinja projects. |
Can we deprecate the |
@@ -64,18 +65,27 @@ class Jinja2Templates: | |||
|
|||
def __init__( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we overload this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the overload and the rebase, we are done here 👍
starlette/templating.py
Outdated
self, | ||
directory: typing.Union[str, PathLike], | ||
**env_options: typing.Any, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self, | |
directory: typing.Union[str, PathLike], | |
**env_options: typing.Any, | |
self, directory: typing.Union[str, PathLike], **env_options: typing.Any |
I've rebased it. |
I had to make all other arguments as kw-only to make overloading work. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks 👍
Let's accept the context_processors
being a kw-only as a breaking change.
@@ -61,7 +61,7 @@ templates.env.filters['marked'] = marked_filter | |||
|
|||
## Using custom jinja2.Environment instance | |||
|
|||
Starlette also accepts a preconfigured `jinja2.Environment` instance. | |||
Starlette also accepts a preconfigured [`jinja2.Environment`](https://jinja.palletsprojects.com/en/3.0.x/api/#api) instance. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this doesn't render very well on mkdocs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok. Great 👍
Thanks 👍 |
Resolves jowilf#574 According to the Starlette 0.28.0 release notes, the **env_options parameter in Jinja2Templates has been deprecated in favor of the new env parameter. The relevant pull request #2159 explains this change. See: - encode/starlette#2159 - https://www.starlette.io/release-notes/#0280
* Ignore .venv * Fix DeprecationWarning: The `name` is not the first parameter anymore. Addresses #574 DeprecationWarning: The `name` is not the first parameter anymore. The first parameter should be the `Request` instance. Replace `TemplateResponse(name, {"request": request})` by `TemplateResponse(request, name)`. warnings.warn( Before Starlette 0.29.0, the name was the first parameter. Also, before that, in previous versions, the request object was passed as part of the key-value pairs in the context for Jinja2. See: - https://www.starlette.io/release-notes/#0290 - encode/starlette#2191 - https://github.com/encode/starlette/blob/c78c9aac17a4d68e0647252310044502f1b7da71/starlette/templating.py#L166-L178 - https://fastapi.tiangolo.com/reference/templating/#fastapi.templating.Jinja2Templates.TemplateResponse - https://fastapi.tiangolo.com/advanced/templates/#using-jinja2templates * Fix DeprecationWarning: Extra environment options are deprecated. Resolves #574 According to the Starlette 0.28.0 release notes, the **env_options parameter in Jinja2Templates has been deprecated in favor of the new env parameter. The relevant pull request #2159 explains this change. See: - encode/starlette#2159 - https://www.starlette.io/release-notes/#0280 * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This PR adds
env
argument toJinja2Templates
class and removes**env_options
.Motivation
See this discussion - #2134
Jinja2Templates
configure only this class and nothing elsejinja2.Environment
and use it in the app without limitsEnvironment
class may be used by other app components, not just by StarletteThis is a breaking change!
Example