简体   繁体   中英

Set the default template filter for the context_processor in flask

In my flask app i have these codes:

from __main__ import app

@app.context_processor
def breakline():
    return { 'break': '<br>' }

How to use in html format is as follows:

{{ break }}

The above code works but there is a problem; A safe filter is needed to detect <br> ; But I do not want to be added in html format every time like this:

{{ break | safe }}

I want this safe filter to be applied automatically and no longer need to be used in the html page. Is such a thing possible?

This is just an example and I do not just want to be able to create a <br> . I want to know if a filter can be added to a context_processor or not.

You can mark your html code as safe by using Markup .

from flask import Markup

@app.context_processor
def breakline():
    return { 'break': Markup('<br>') }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM