Skip to content

Allow custom filters for Jinja2#737

Merged
lovelydinosaur merged 1 commit intoKludex:masterfrom
eillarra:jinja2_custom_custom_filters
Dec 10, 2019
Merged

Allow custom filters for Jinja2#737
lovelydinosaur merged 1 commit intoKludex:masterfrom
eillarra:jinja2_custom_custom_filters

Conversation

@eillarra
Copy link
Contributor

@eillarra eillarra commented Nov 26, 2019

We could change templates.env directly, but I am not sure what the preferred way is.
If you prefer that we add filters directly changing the env property, then at least we should update the documentation.

Option 1 (included in this pull request):

from commonmark import commonmark
from starlette.templating import Jinja2Templates

def marked_filter(text):
    return commonmark(text)

templates = Jinja2Templates(directory='templates', custom_filters={
    'marked': marked_filter
})

Option 2 (only needs updating the documentation):

from commonmark import commonmark
from starlette.templating import Jinja2Templates

def marked_filter(text):
    return commonmark(text)

templates = Jinja2Templates(directory='templates')
templates.env.filters['marked'] = marked_filter

Let me know what you think.

@lovelydinosaur
Copy link

Option 2 sounds good here.

@eillarra
Copy link
Contributor Author

eillarra commented Nov 26, 2019

Ok, I have squashed the commits: it only includes a note in the docs now.

@alex-oleshkevich
Copy link
Contributor

Jinja2Templates also can pass **kwargs to the Environment.

@eillarra eillarra deleted the jinja2_custom_custom_filters branch December 10, 2019 15:08
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