Skip to content

Add template context processors.#1904

Merged
alex-oleshkevich merged 20 commits intoKludex:masterfrom
alex-oleshkevich:context-processors
Dec 29, 2022
Merged

Add template context processors.#1904
alex-oleshkevich merged 20 commits intoKludex:masterfrom
alex-oleshkevich:context-processors

Conversation

@alex-oleshkevich
Copy link
Contributor

This PR adds template context processors.

import typing

from starlette.requests import Request
from starlette.templating import Jinja2Templates

def app_context(request: Request) -> typing.Dict[str, typing.Any]:
    return {'app': request.app}

templates = Jinja2Templates(directory='templates', context_processors=[
    app_context,
])

Async processors are not supported.

@Kludex
Copy link
Owner

Kludex commented Oct 12, 2022

What's the motivation for it? You can still perform an analogous logic without introducing the context_processors, correct?

@alex-oleshkevich
Copy link
Contributor Author

Yes, by copypasting. If you share variables with all templates (for example, teams in sidebar, account balance, etc) you have to generate and pass these variables to the template in every view. Context processors solve this problem. You define login in one place and template's context get automatically updated.

Copy link
Owner

@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.

@Kludex Kludex added feature New feature or request staticfiles Static file serving labels Dec 25, 2022
alex-oleshkevich and others added 6 commits December 25, 2022 21:41
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Kludex
Kludex previously approved these changes Dec 25, 2022
Copy link
Owner

@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.

I wrote a suggestion to simplify a bit the test, and the documentation.

Everything cool here. 👍

alex-oleshkevich and others added 3 commits December 26, 2022 00:00
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
@alex-oleshkevich
Copy link
Contributor Author

Are you waiting for anything from my side, @Kludex ?

@Kludex
Copy link
Owner

Kludex commented Dec 29, 2022

I was kind a waiting for you to press merge 👀

@alex-oleshkevich alex-oleshkevich merged commit 008e3a5 into Kludex:master Dec 29, 2022
@alex-oleshkevich alex-oleshkevich deleted the context-processors branch December 29, 2022 14:41
invisibleroads added a commit to invisibleroads/invisibleroads-macros-web that referenced this pull request Feb 11, 2023
aminalaee pushed a commit that referenced this pull request Feb 13, 2023
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Viicos pushed a commit to Viicos/starlette that referenced this pull request Feb 16, 2023
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request staticfiles Static file serving

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants