How to enable automatic caching in templates with .jhtml extension in Flask?

Flags Status :

autoescaping is allowed for all templates ending in .html, .htm, .xml, and .xhtml

How to enable autoescaping for templates ending with .jhtml extension?

+4
source share
2 answers

Thanks to @Cagez's answer, I was able to find a reliable (and recommended) solution.

According to a message on the Flask mailing list , a way to do this is to override flask.Flask.select_jinja_autoescape() . The related snippet in this post demonstrating how to override which autoescaped patterns does not quite work, so I came up with the following that does the job:

 class JHtmlEscapingFlask(Flask): def select_jinja_autoescape(self, filename): if filename is None: return False if filename.endswith('.jhtml'): return True return Flask.select_jinja_autoescape(self, filename) app = JHtmlEscapingFlask(__name__) 

I put this at the top of my main Flask application file, replacing the usual app = Flask(__name__)

+3
source

It looks like you can install the file extensions using the "autoescape" option when creating the application. Take a look at the create_jinja_environment () method at https://github.com/mitsuhiko/flask/blob/master/flask/app.py

+5
source

Source: https://habr.com/ru/post/1443993/


All Articles