Home > database >  Set the default template filter for the context_processor in flask
Set the default template filter for the context_processor in flask

Time:10-31

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.

CodePudding user response:

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

from flask import Markup

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