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

Add env argument to Jinja2Templates, remove **env_options. (ref #2134) #2159

Merged
merged 17 commits into from
Jun 5, 2023

Conversation

alex-oleshkevich
Copy link
Member

This PR adds env argument to Jinja2Templates class and removes **env_options.

Motivation
See this discussion - #2134

  1. arguments of Jinja2Templates configure only this class and nothing else
  2. users can configure jinja2.Environment and use it in the app without limits
  3. the Environment class may be used by other app components, not just by Starlette

This is a breaking change!

Example

import jinja2
from starlette.templating import Jinja2Templates

env = jinja2.Environment(...)
templates = Jinja2Templates(env=env)

@NickColley
Copy link

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.

@Kludex
Copy link
Member

Kludex commented May 31, 2023

Can we deprecate the **env_options first, then remove it on v1?

@@ -64,18 +65,27 @@ class Jinja2Templates:

def __init__(
Copy link
Member

Choose a reason for hiding this comment

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

Can we overload this?

Copy link
Member Author

Choose a reason for hiding this comment

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

good idea

Copy link
Member

@Kludex Kludex left a 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 👍

Comment on lines 86 to 92
self,
directory: typing.Union[str, PathLike],
**env_options: typing.Any,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
self,
directory: typing.Union[str, PathLike],
**env_options: typing.Any,
self, directory: typing.Union[str, PathLike], **env_options: typing.Any

tests/test_templates.py Show resolved Hide resolved
@Kludex
Copy link
Member

Kludex commented Jun 3, 2023

I've rebased it.

@Kludex Kludex mentioned this pull request Jun 4, 2023
@alex-oleshkevich
Copy link
Member Author

I had to make all other arguments as kw-only to make overloading work.

Copy link
Member

@Kludex Kludex left a 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.

docs/templates.md Outdated Show resolved Hide resolved
@@ -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.
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

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

it looks good
image

Copy link
Member

Choose a reason for hiding this comment

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

Ah ok. Great 👍

@Kludex
Copy link
Member

Kludex commented Jun 5, 2023

Thanks 👍

@Kludex Kludex merged commit d155d3b into encode:master Jun 5, 2023
ptrstn added a commit to ptrstn/starlette-admin that referenced this pull request Aug 31, 2024
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
jowilf pushed a commit to jowilf/starlette-admin that referenced this pull request Aug 31, 2024
* 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>
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.

3 participants