The Flask docs state:
autoescaping is enabled for all templates ending in .html, .htm, .xml as well as .xhtml
How do I enable autoescaping for templates ending with a .jhtml extension?
It looks like you can set the file extensions with the 'autoescape' option when creating the app. Take a look at the create_jinja_environment() method at https://github.com/mitsuhiko/flask/blob/master/flask/app.py
Thanks to @Cagez's answer I was able to come up with a robust (and recommended) solution.
According to a post on the Flask mailing list, the way to do this is to override flask.Flask.select_jinja_autoescape()
. The linked snippet in that post demonstrating how to override which templates are autoescaped didn't quite work, so I came up with the following, which does work:
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 app file, replacing the usual app = Flask(__name__)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With