Skip to content

Add template context processors. - #1904

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

alex-oleshkevich merged 20 commits into
Kludex:masterfrom
alex-oleshkevich:context-processors

Conversation

@alex-oleshkevich

Copy link
Copy Markdown
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

Kludex commented Oct 12, 2022

Copy link
Copy Markdown
Owner

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

@alex-oleshkevich

Copy link
Copy Markdown
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.

@Kludex Kludex left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Comment thread docs/templates.md Outdated
Comment thread docs/templates.md Outdated
Comment thread docs/templates.md Outdated
Comment thread docs/templates.md Outdated
Comment thread docs/templates.md Outdated
Comment thread docs/templates.md
Comment thread docs/templates.md Outdated
Comment thread docs/templates.md Outdated
@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 <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Kludex
Kludex previously approved these changes Dec 25, 2022

@Kludex Kludex left a comment

Copy link
Copy Markdown
Owner

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. 馃憤

Comment thread docs/templates.md Outdated
Comment thread tests/test_templates.py Outdated
Comment thread tests/test_templates.py Outdated
alex-oleshkevich and others added 3 commits December 26, 2022 00:00
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Co-authored-by: Marcelo Trylesinski <[email protected]>
Comment thread tests/test_templates.py Outdated
Co-authored-by: Marcelo Trylesinski <[email protected]>
@alex-oleshkevich

Copy link
Copy Markdown
Contributor Author

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

@Kludex

Kludex commented Dec 29, 2022

Copy link
Copy Markdown
Owner

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
Viicos pushed a commit to Viicos/starlette that referenced this pull request Feb 16, 2023
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