Turning on Jinja autoescaping when using Template() directly
September 18, 2020 ยท View on GitHub
Jinja autoescaping is turned off by default. Coming from Django this frequently catches me out.
You can turn on autoescaping for your Jinja environment using:
from jinja2 import Environment, FileSystemLoader
env = Environment(
loader=FileSystemLoader("/path/to/templates"),
autoescape=True
)
But what about if you are using Template directly? TIL that the Template class takes all of the same options as Environment does, so you can do this:
from jinja2 import Template
template = Template("""
<p>Hello {{ name }}</p>
""", autoescape=True)
print(template.render({"name": "Simon & Cleo"}))
# Output: <p>Hello Simon & Cleo</p>
Here's the Template class constructor.