<h1>Templates<aclass="headerlink"href="#templates"title="Link to this heading">¶</a></h1>
<p>Flask leverages Jinja2 as its template engine. You are obviously free to use
a different template engine, but you still have to install Jinja2 to run
Flask itself. This requirement is necessary to enable rich extensions.
An extension can depend on Jinja2 being present.</p>
<p>This section only gives a very quick introduction into how Jinja2
is integrated into Flask. If you want information on the template
engine’s syntax itself, head over to the official <aclass="reference external"href="https://jinja.palletsprojects.com/templates/">Jinja2 Template
Documentation</a> for
more information.</p>
<sectionid="jinja-setup">
<h2>Jinja Setup<aclass="headerlink"href="#jinja-setup"title="Link to this heading">¶</a></h2>
<p>Unless customized, Jinja2 is configured by Flask as follows:</p>
<ulclass="simple">
<li><p>autoescaping is enabled for all templates ending in <codeclass="docutils literal notranslate"><spanclass="pre">.html</span></code>,
<codeclass="docutils literal notranslate"><spanclass="pre">.htm</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">.xml</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">.xhtml</span></code>, as well as <codeclass="docutils literal notranslate"><spanclass="pre">.svg</span></code> when using
<dd><p>The current request object (<aclass="reference internal"href="api.html#flask.request"title="flask.request"><codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">flask.request</span></code></a>). This variable is
unavailable if the template was rendered without an active request
<dd><p>The request-bound object for global variables (<aclass="reference internal"href="api.html#flask.g"title="flask.g"><codeclass="xref py py-data docutils literal notranslate"><spanclass="pre">flask.g</span></code></a>). This
variable is unavailable if the template was rendered without an active
<h2>Controlling Autoescaping<aclass="headerlink"href="#controlling-autoescaping"title="Link to this heading">¶</a></h2>
<p>Autoescaping is the concept of automatically escaping special characters
for you. Special characters in the sense of HTML (or XML, and thus XHTML)
are <codeclass="docutils literal notranslate"><spanclass="pre">&</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">></span></code>, <codeclass="docutils literal notranslate"><spanclass="pre"><</span></code>, <codeclass="docutils literal notranslate"><spanclass="pre">"</span></code> as well as <codeclass="docutils literal notranslate"><spanclass="pre">'</span></code>. Because these characters
carry specific meanings in documents on their own you have to replace them
by so called “entities” if you want to use them for text. Not doing so
would not only cause user frustration by the inability to use these
characters in text, but can also lead to security problems. (see
<p>Sometimes however you will need to disable autoescaping in templates.
This can be the case if you want to explicitly inject HTML into pages, for
example if they come from a system that generates secure HTML like a
markdown to HTML converter.</p>
<p>There are three ways to accomplish that:</p>
<ulclass="simple">
<li><p>In the Python code, wrap the HTML string in a <codeclass="xref py py-class docutils literal notranslate"><spanclass="pre">Markup</span></code>
object before passing it to the template. This is in general the
recommended way.</p></li>
<li><p>Inside the template, use the <codeclass="docutils literal notranslate"><spanclass="pre">|safe</span></code> filter to explicitly mark a
string as safe HTML (<codeclass="docutils literal notranslate"><spanclass="pre">{{</span><spanclass="pre">myvariable|safe</span><spanclass="pre">}}</span></code>)</p></li>
<li><p>Temporarily disable the autoescape system altogether.</p></li>
</ul>
<p>To disable the autoescape system in templates, you can use the <codeclass="docutils literal notranslate"><spanclass="pre">{%</span>
<p>Whenever you do this, please be very cautious about the variables you are
using in this block.</p>
</section>
<sectionid="registering-filters">
<spanid="id1"></span><h2>Registering Filters<aclass="headerlink"href="#registering-filters"title="Link to this heading">¶</a></h2>
<p>If you want to register your own filters in Jinja2 you have two ways to do
that. You can either put them by hand into the
<aclass="reference internal"href="api.html#flask.Flask.jinja_env"title="flask.Flask.jinja_env"><codeclass="xref py py-attr docutils literal notranslate"><spanclass="pre">jinja_env</span></code></a> of the application or use the
<p>The context processor above makes the <codeclass="code docutils literal notranslate"><spanclass="pre">format_price</span></code> function available to all
<p>You could also build <codeclass="code docutils literal notranslate"><spanclass="pre">format_price</span></code> as a template filter (see
<aclass="reference internal"href="#registering-filters"><spanclass="std std-ref">Registering Filters</span></a>), but this demonstrates how to pass functions in a
context processor.</p>
</section>
<sectionid="streaming">
<h2>Streaming<aclass="headerlink"href="#streaming"title="Link to this heading">¶</a></h2>
<p>It can be useful to not render the whole template as one complete
string, instead render it as a stream, yielding smaller incremental
strings. This can be used for streaming HTML in chunks to speed up
initial page load, or to save memory when rendering a very large
template.</p>
<p>The Jinja2 template engine supports rendering a template piece
by piece, returning an iterator of strings. Flask provides the
<aclass="reference internal"href="api.html#flask.stream_with_context"title="flask.stream_with_context"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">stream_with_context()</span></code></a> wrapper if a request is active, so