Add env argument to Jinja2Templates, remove **env_options. (ref #2134)#2159
Add env argument to Jinja2Templates, remove **env_options. (ref #2134)#2159Kludex merged 17 commits intoKludex:masterfrom
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__( | |||
Kludex
left a comment
There was a problem hiding this comment.
With the overload and the rebase, we are done here 👍
| self, | ||
| directory: typing.Union[str, PathLike], | ||
| **env_options: typing.Any, |
There was a problem hiding this comment.
| 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. |
Kludex
left a comment
There was a problem hiding this comment.
Thanks 👍
Let's accept the context_processors being a kw-only as a breaking change.
| ## 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.
I think this doesn't render very well on mkdocs
|
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: - Kludex/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 - Kludex/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: - Kludex/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
envargument toJinja2Templatesclass and removes**env_options.Motivation
See this discussion - #2134
Jinja2Templatesconfigure only this class and nothing elsejinja2.Environmentand use it in the app without limitsEnvironmentclass may be used by other app components, not just by StarletteThis is a breaking change!
Example