Hacked By AnonymousFox
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Template Designer Documentation — Jinja2 2.10.1 documentation</title>
<link rel="stylesheet" href="_static/jinja.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Extensions" href="extensions.html" />
<link rel="prev" title="Native Python Types" href="nativetypes.html" />
</head><body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="extensions.html" title="Extensions"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="nativetypes.html" title="Native Python Types"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Jinja2 2.10.1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="template-designer-documentation">
<h1>Template Designer Documentation<a class="headerlink" href="#template-designer-documentation" title="Permalink to this headline">¶</a></h1>
<p>This document describes the syntax and semantics of the template engine and
will be most useful as reference to those creating Jinja templates. As the
template engine is very flexible, the configuration from the application can
be slightly different from the code presented here in terms of delimiters and
behavior of undefined values.</p>
<div class="section" id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Permalink to this headline">¶</a></h2>
<p>A Jinja template is simply a text file. Jinja can generate any text-based
format (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn’t need to have a
specific extension: <code class="docutils literal notranslate"><span class="pre">.html</span></code>, <code class="docutils literal notranslate"><span class="pre">.xml</span></code>, or any other extension is just fine.</p>
<p>A template contains <strong>variables</strong> and/or <strong>expressions</strong>, which get replaced
with values when a template is <em>rendered</em>; and <strong>tags</strong>, which control the
logic of the template. The template syntax is heavily inspired by Django and
Python.</p>
<p>Below is a minimal template that illustrates a few basics using the default
Jinja configuration. We will cover the details later in this document:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp"><!DOCTYPE html></span>
<span class="p"><</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">"en"</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span>My Webpage<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">ul</span> <span class="na">id</span><span class="o">=</span><span class="s">"navigation"</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">navigation</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">item.href</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">item.caption</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">a</span><span class="p">></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="p"><</span><span class="nt">h1</span><span class="p">></span>My Webpage<span class="p"></</span><span class="nt">h1</span><span class="p">></span>
<span class="cp">{{</span> <span class="nv">a_variable</span> <span class="cp">}}</span>
<span class="c">{# a comment #}</span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</pre></div>
</div>
<p>The following example shows the default configuration settings. An application
developer can change the syntax configuration from <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">foo</span> <span class="pre">%}</span></code> to <code class="docutils literal notranslate"><span class="pre"><%</span> <span class="pre">foo</span>
<span class="pre">%></span></code>, or something similar.</p>
<p>There are a few kinds of delimiters. The default Jinja delimiters are
configured as follows:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">...</span> <span class="pre">%}</span></code> for <a class="reference internal" href="#list-of-control-structures"><span class="std std-ref">Statements</span></a></li>
<li><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">...</span> <span class="pre">}}</span></code> for <a class="reference internal" href="#expressions"><span class="std std-ref">Expressions</span></a> to print to the template output</li>
<li><code class="docutils literal notranslate"><span class="pre">{#</span> <span class="pre">...</span> <span class="pre">#}</span></code> for <a class="reference internal" href="#comments"><span class="std std-ref">Comments</span></a> not included in the template output</li>
<li><code class="docutils literal notranslate"><span class="pre">#</span>  <span class="pre">...</span> <span class="pre">##</span></code> for <a class="reference internal" href="#line-statements"><span class="std std-ref">Line Statements</span></a></li>
</ul>
</div>
<div class="section" id="variables">
<span id="id1"></span><h2>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h2>
<p>Template variables are defined by the context dictionary passed to the
template.</p>
<p>You can mess around with the variables in templates provided they are passed in
by the application. Variables may have attributes or elements on them you can
access too. What attributes a variable has depends heavily on the application
providing that variable.</p>
<p>You can use a dot (<code class="docutils literal notranslate"><span class="pre">.</span></code>) to access attributes of a variable in addition
to the standard Python <code class="docutils literal notranslate"><span class="pre">__getitem__</span></code> “subscript” syntax (<code class="docutils literal notranslate"><span class="pre">[]</span></code>).</p>
<p>The following lines do the same thing:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">foo.bar</span> <span class="cp">}}</span>
<span class="cp">{{</span> <span class="nv">foo</span><span class="o">[</span><span class="s1">'bar'</span><span class="o">]</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>It’s important to know that the outer double-curly braces are <em>not</em> part of the
variable, but the print statement. If you access variables inside tags don’t
put the braces around them.</p>
<p>If a variable or attribute does not exist, you will get back an undefined
value. What you can do with that kind of value depends on the application
configuration: the default behavior is to evaluate to an empty string if
printed or iterated over, and to fail for every other operation.</p>
<div class="admonition-implementation admonition" id="notes-on-subscriptions">
<p class="first admonition-title">Implementation</p>
<p>For the sake of convenience, <code class="docutils literal notranslate"><span class="pre">foo.bar</span></code> in Jinja2 does the following
things on the Python layer:</p>
<ul class="simple">
<li>check for an attribute called <cite>bar</cite> on <cite>foo</cite>
(<code class="docutils literal notranslate"><span class="pre">getattr(foo,</span> <span class="pre">'bar')</span></code>)</li>
<li>if there is not, check for an item <code class="docutils literal notranslate"><span class="pre">'bar'</span></code> in <cite>foo</cite>
(<code class="docutils literal notranslate"><span class="pre">foo.__getitem__('bar')</span></code>)</li>
<li>if there is not, return an undefined object.</li>
</ul>
<p><code class="docutils literal notranslate"><span class="pre">foo['bar']</span></code> works mostly the same with a small difference in sequence:</p>
<ul class="simple">
<li>check for an item <code class="docutils literal notranslate"><span class="pre">'bar'</span></code> in <cite>foo</cite>.
(<code class="docutils literal notranslate"><span class="pre">foo.__getitem__('bar')</span></code>)</li>
<li>if there is not, check for an attribute called <cite>bar</cite> on <cite>foo</cite>.
(<code class="docutils literal notranslate"><span class="pre">getattr(foo,</span> <span class="pre">'bar')</span></code>)</li>
<li>if there is not, return an undefined object.</li>
</ul>
<p class="last">This is important if an object has an item and attribute with the same
name. Additionally, the <a class="reference internal" href="#attr" title="attr"><code class="xref py py-func docutils literal notranslate"><span class="pre">attr()</span></code></a> filter only looks up attributes.</p>
</div>
</div>
<div class="section" id="filters">
<span id="id2"></span><h2>Filters<a class="headerlink" href="#filters" title="Permalink to this headline">¶</a></h2>
<p>Variables can be modified by <strong>filters</strong>. Filters are separated from the
variable by a pipe symbol (<code class="docutils literal notranslate"><span class="pre">|</span></code>) and may have optional arguments in
parentheses. Multiple filters can be chained. The output of one filter is
applied to the next.</p>
<p>For example, <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">name|striptags|title</span> <span class="pre">}}</span></code> will remove all HTML Tags from
variable <cite>name</cite> and title-case the output (<code class="docutils literal notranslate"><span class="pre">title(striptags(name))</span></code>).</p>
<p>Filters that accept arguments have parentheses around the arguments, just like
a function call. For example: <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">listx|join(',</span> <span class="pre">')</span> <span class="pre">}}</span></code> will join a list with
commas (<code class="docutils literal notranslate"><span class="pre">str.join(',</span> <span class="pre">',</span> <span class="pre">listx)</span></code>).</p>
<p>The <a class="reference internal" href="#builtin-filters"><span class="std std-ref">List of Builtin Filters</span></a> below describes all the builtin filters.</p>
</div>
<div class="section" id="tests">
<span id="id3"></span><h2>Tests<a class="headerlink" href="#tests" title="Permalink to this headline">¶</a></h2>
<p>Beside filters, there are also so-called “tests” available. Tests can be used
to test a variable against a common expression. To test a variable or
expression, you add <cite>is</cite> plus the name of the test after the variable. For
example, to find out if a variable is defined, you can do <code class="docutils literal notranslate"><span class="pre">name</span> <span class="pre">is</span> <span class="pre">defined</span></code>,
which will then return true or false depending on whether <cite>name</cite> is defined
in the current template context.</p>
<p>Tests can accept arguments, too. If the test only takes one argument, you can
leave out the parentheses. For example, the following two
expressions do the same thing:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nb">loop</span><span class="nv">.index</span> <span class="k">is</span> <span class="nf">divisibleby</span> <span class="m">3</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nb">loop</span><span class="nv">.index</span> <span class="k">is</span> <span class="nf">divisibleby</span><span class="o">(</span><span class="m">3</span><span class="o">)</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#builtin-tests"><span class="std std-ref">List of Builtin Tests</span></a> below describes all the builtin tests.</p>
</div>
<div class="section" id="comments">
<span id="id4"></span><h2>Comments<a class="headerlink" href="#comments" title="Permalink to this headline">¶</a></h2>
<p>To comment-out part of a line in a template, use the comment syntax which is
by default set to <code class="docutils literal notranslate"><span class="pre">{#</span> <span class="pre">...</span> <span class="pre">#}</span></code>. This is useful to comment out parts of the
template for debugging or to add information for other template designers or
yourself:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="c">{# note: commented-out template because we no longer use this</span>
<span class="c"> {% for user in users %}</span>
<span class="c"> ...</span>
<span class="c"> {% endfor %}</span>
<span class="c">#}</span>
</pre></div>
</div>
</div>
<div class="section" id="whitespace-control">
<h2>Whitespace Control<a class="headerlink" href="#whitespace-control" title="Permalink to this headline">¶</a></h2>
<p>In the default configuration:</p>
<ul class="simple">
<li>a single trailing newline is stripped if present</li>
<li>other whitespace (spaces, tabs, newlines etc.) is returned unchanged</li>
</ul>
<p>If an application configures Jinja to <cite>trim_blocks</cite>, the first newline after a
template tag is removed automatically (like in PHP). The <cite>lstrip_blocks</cite>
option can also be set to strip tabs and spaces from the beginning of a
line to the start of a block. (Nothing will be stripped if there are
other characters before the start of the block.)</p>
<p>With both <cite>trim_blocks</cite> and <cite>lstrip_blocks</cite> enabled, you can put block tags
on their own lines, and the entire block line will be removed when
rendered, preserving the whitespace of the contents. For example,
without the <cite>trim_blocks</cite> and <cite>lstrip_blocks</cite> options, this template:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">div</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">if</span> <span class="kp">True</span> <span class="cp">%}</span>
yay
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
</pre></div>
</div>
<p>gets rendered with blank lines inside the div:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">div</span><span class="p">></span>
yay
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
</pre></div>
</div>
<p>But with both <cite>trim_blocks</cite> and <cite>lstrip_blocks</cite> enabled, the template block
lines are removed and other whitespace is preserved:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">div</span><span class="p">></span>
yay
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
</pre></div>
</div>
<p>You can manually disable the <cite>lstrip_blocks</cite> behavior by putting a
plus sign (<code class="docutils literal notranslate"><span class="pre">+</span></code>) at the start of a block:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">div</span><span class="p">></span>
{%+ if something %}yay<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
</pre></div>
</div>
<p>You can also strip whitespace in templates by hand. If you add a minus
sign (<code class="docutils literal notranslate"><span class="pre">-</span></code>) to the start or end of a block (e.g. a <a class="reference internal" href="#for-loop"><span class="std std-ref">For</span></a> tag), a
comment, or a variable expression, the whitespaces before or after
that block will be removed:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">seq</span> -<span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">item</span> <span class="cp">}}</span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This will yield all elements without whitespace between them. If <cite>seq</cite> was
a list of numbers from <code class="docutils literal notranslate"><span class="pre">1</span></code> to <code class="docutils literal notranslate"><span class="pre">9</span></code>, the output would be <code class="docutils literal notranslate"><span class="pre">123456789</span></code>.</p>
<p>If <a class="reference internal" href="#line-statements"><span class="std std-ref">Line Statements</span></a> are enabled, they strip leading whitespace
automatically up to the beginning of the line.</p>
<p>By default, Jinja2 also removes trailing newlines. To keep single
trailing newlines, configure Jinja to <cite>keep_trailing_newline</cite>.</p>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p>You must not add whitespace between the tag and the minus sign.</p>
<p><strong>valid</strong>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span>- <span class="k">if</span> <span class="nv">foo</span> -<span class="cp">%}</span>...<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p><strong>invalid</strong>:</p>
<div class="last highlight-html+jinja notranslate"><div class="highlight"><pre><span></span>{% - if foo - %}...<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="escaping">
<h2>Escaping<a class="headerlink" href="#escaping" title="Permalink to this headline">¶</a></h2>
<p>It is sometimes desirable – even necessary – to have Jinja ignore parts
it would otherwise handle as variables or blocks. For example, if, with
the default syntax, you want to use <code class="docutils literal notranslate"><span class="pre">{{</span></code> as a raw string in a template and
not start a variable, you have to use a trick.</p>
<p>The easiest way to output a literal variable delimiter (<code class="docutils literal notranslate"><span class="pre">{{</span></code>) is by using a
variable expression:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s1">'{{'</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>For bigger sections, it makes sense to mark a block <cite>raw</cite>. For example, to
include example Jinja syntax in a template, you can use this snippet:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">raw</span> <span class="cp">%}</span>
<ul>
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
</ul>
<span class="cp">{%</span> <span class="k">endraw</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="line-statements">
<span id="id5"></span><h2>Line Statements<a class="headerlink" href="#line-statements" title="Permalink to this headline">¶</a></h2>
<p>If line statements are enabled by the application, it’s possible to mark a
line as a statement. For example, if the line statement prefix is configured
to <code class="docutils literal notranslate"><span class="pre">#</span></code>, the following two examples are equivalent:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
# for item in seq
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">item</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
# endfor
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">seq</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">item</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>The line statement prefix can appear anywhere on the line as long as no text
precedes it. For better readability, statements that start a block (such as
<cite>for</cite>, <cite>if</cite>, <cite>elif</cite> etc.) may end with a colon:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span># for item in seq:
...
# endfor
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p>Line statements can span multiple lines if there are open parentheses,
braces or brackets:</p>
<div class="last highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
# for href, caption in [('index.html', 'Index'),
('about.html', 'About')]:
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">href</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">caption</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">a</span><span class="p">></</span><span class="nt">li</span><span class="p">></span>
# endfor
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
</div>
<p>Since Jinja 2.2, line-based comments are available as well. For example, if
the line-comment prefix is configured to be <code class="docutils literal notranslate"><span class="pre">##</span></code>, everything from <code class="docutils literal notranslate"><span class="pre">##</span></code> to
the end of the line is ignored (excluding the newline sign):</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span># for item in seq:
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">item</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span> ## this comment is ignored
# endfor
</pre></div>
</div>
</div>
<div class="section" id="template-inheritance">
<span id="id6"></span><h2>Template Inheritance<a class="headerlink" href="#template-inheritance" title="Permalink to this headline">¶</a></h2>
<p>The most powerful part of Jinja is template inheritance. Template inheritance
allows you to build a base “skeleton” template that contains all the common
elements of your site and defines <strong>blocks</strong> that child templates can override.</p>
<p>Sounds complicated but is very basic. It’s easiest to understand it by starting
with an example.</p>
<div class="section" id="base-template">
<h3>Base Template<a class="headerlink" href="#base-template" title="Permalink to this headline">¶</a></h3>
<p>This template, which we’ll call <code class="docutils literal notranslate"><span class="pre">base.html</span></code>, defines a simple HTML skeleton
document that you might use for a simple two-column page. It’s the job of
“child” templates to fill the empty blocks with content:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp"><!DOCTYPE html></span>
<span class="p"><</span><span class="nt">html</span> <span class="na">lang</span><span class="o">=</span><span class="s">"en"</span><span class="p">></span>
<span class="p"><</span><span class="nt">head</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">head</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">link</span> <span class="na">rel</span><span class="o">=</span><span class="s">"stylesheet"</span> <span class="na">href</span><span class="o">=</span><span class="s">"style.css"</span> <span class="p">/></span>
<span class="p"><</span><span class="nt">title</span><span class="p">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span> - My Webpage<span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">head</span><span class="p">></span>
<span class="p"><</span><span class="nt">body</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">"content"</span><span class="p">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">"footer"</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">footer</span> <span class="cp">%}</span>
<span class="ni">&copy;</span> Copyright 2008 by <span class="p"><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"http://domain.invalid/"</span><span class="p">></span>you<span class="p"></</span><span class="nt">a</span><span class="p">></span>.
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"></</span><span class="nt">body</span><span class="p">></span>
<span class="p"></</span><span class="nt">html</span><span class="p">></span>
</pre></div>
</div>
<p>In this example, the <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></code> tags define four blocks that child templates
can fill in. All the <cite>block</cite> tag does is tell the template engine that a
child template may override those placeholders in the template.</p>
</div>
<div class="section" id="child-template">
<h3>Child Template<a class="headerlink" href="#child-template" title="Permalink to this headline">¶</a></h3>
<p>A child template might look like this:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"base.html"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}</span>Index<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">head</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nb">super</span><span class="o">()</span> <span class="cp">}}</span>
<span class="p"><</span><span class="nt">style</span> <span class="na">type</span><span class="o">=</span><span class="s">"text/css"</span><span class="p">></span>
<span class="p">.</span><span class="nc">important</span> <span class="p">{</span> <span class="k">color</span><span class="p">:</span> <span class="mh">#336699</span><span class="p">;</span> <span class="p">}</span>
<span class="p"></</span><span class="nt">style</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">content</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">h1</span><span class="p">></span>Index<span class="p"></</span><span class="nt">h1</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span> <span class="na">class</span><span class="o">=</span><span class="s">"important"</span><span class="p">></span>
Welcome to my awesome homepage.
<span class="p"></</span><span class="nt">p</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">extends</span> <span class="pre">%}</span></code> tag is the key here. It tells the template engine that
this template “extends” another template. When the template system evaluates
this template, it first locates the parent. The extends tag should be the
first tag in the template. Everything before it is printed out normally and
may cause confusion. For details about this behavior and how to take
advantage of it, see <a class="reference internal" href="tricks.html#null-master-fallback"><span class="std std-ref">Null-Master Fallback</span></a>.</p>
<p>The filename of the template depends on the template loader. For example, the
<code class="xref py py-class docutils literal notranslate"><span class="pre">FileSystemLoader</span></code> allows you to access other templates by giving the
filename. You can access templates in subdirectories with a slash:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="s2">"layout/default.html"</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>But this behavior can depend on the application embedding Jinja. Note that
since the child template doesn’t define the <code class="docutils literal notranslate"><span class="pre">footer</span></code> block, the value from
the parent template is used instead.</p>
<p>You can’t define multiple <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></code> tags with the same name in the
same template. This limitation exists because a block tag works in “both”
directions. That is, a block tag doesn’t just provide a placeholder to fill
- it also defines the content that fills the placeholder in the <em>parent</em>.
If there were two similarly-named <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">block</span> <span class="pre">%}</span></code> tags in a template,
that template’s parent wouldn’t know which one of the blocks’ content to use.</p>
<p>If you want to print a block multiple times, you can, however, use the special
<cite>self</cite> variable and call the block with that name:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">title</span><span class="p">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">title</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">title</span><span class="p">></span>
<span class="p"><</span><span class="nt">h1</span><span class="p">></span><span class="cp">{{</span> <span class="nv">self.title</span><span class="o">()</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">h1</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">body</span> <span class="cp">%}{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="super-blocks">
<h3>Super Blocks<a class="headerlink" href="#super-blocks" title="Permalink to this headline">¶</a></h3>
<p>It’s possible to render the contents of the parent block by calling <cite>super</cite>.
This gives back the results of the parent block:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">sidebar</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">h3</span><span class="p">></span>Table Of Contents<span class="p"></</span><span class="nt">h3</span><span class="p">></span>
...
<span class="cp">{{</span> <span class="nb">super</span><span class="o">()</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="named-block-end-tags">
<h3>Named Block End-Tags<a class="headerlink" href="#named-block-end-tags" title="Permalink to this headline">¶</a></h3>
<p>Jinja2 allows you to put the name of the block after the end tag for better
readability:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">sidebar</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">block</span> <span class="nv">inner_sidebar</span> <span class="cp">%}</span>
...
<span class="cp">{%</span> <span class="k">endblock</span> <span class="nv">inner_sidebar</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endblock</span> <span class="nv">sidebar</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>However, the name after the <cite>endblock</cite> word must match the block name.</p>
</div>
<div class="section" id="block-nesting-and-scope">
<h3>Block Nesting and Scope<a class="headerlink" href="#block-nesting-and-scope" title="Permalink to this headline">¶</a></h3>
<p>Blocks can be nested for more complex layouts. However, per default blocks
may not access variables from outer scopes:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">seq</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">loop_item</span> <span class="cp">%}{{</span> <span class="nv">item</span> <span class="cp">}}{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>This example would output empty <code class="docutils literal notranslate"><span class="pre"><li></span></code> items because <cite>item</cite> is unavailable
inside the block. The reason for this is that if the block is replaced by
a child template, a variable would appear that was not defined in the block or
passed to the context.</p>
<p>Starting with Jinja 2.2, you can explicitly specify that variables are
available in a block by setting the block to “scoped” by adding the <cite>scoped</cite>
modifier to a block declaration:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">seq</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{%</span> <span class="k">block</span> <span class="nv">loop_item</span> <span class="k">scoped</span> <span class="cp">%}{{</span> <span class="nv">item</span> <span class="cp">}}{%</span> <span class="k">endblock</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>When overriding a block, the <cite>scoped</cite> modifier does not have to be provided.</p>
</div>
<div class="section" id="template-objects">
<h3>Template Objects<a class="headerlink" href="#template-objects" title="Permalink to this headline">¶</a></h3>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4.</span></p>
</div>
<p>If a template object was passed in the template context, you can
extend from that object as well. Assuming the calling code passes
a layout template as <cite>layout_template</cite> to the environment, this
code works:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="nv">layout_template</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Previously, the <cite>layout_template</cite> variable had to be a string with
the layout template’s filename for this to work.</p>
</div>
</div>
<div class="section" id="html-escaping">
<h2>HTML Escaping<a class="headerlink" href="#html-escaping" title="Permalink to this headline">¶</a></h2>
<p>When generating HTML from templates, there’s always a risk that a variable will
include characters that affect the resulting HTML. There are two approaches:</p>
<ol class="loweralpha simple">
<li>manually escaping each variable; or</li>
<li>automatically escaping everything by default.</li>
</ol>
<p>Jinja supports both. What is used depends on the application configuration.
The default configuration is no automatic escaping; for various reasons:</p>
<ul class="simple">
<li>Escaping everything except for safe values will also mean that Jinja is
escaping variables known to not include HTML (e.g. numbers, booleans)
which can be a huge performance hit.</li>
<li>The information about the safety of a variable is very fragile. It could
happen that by coercing safe and unsafe values, the return value is
double-escaped HTML.</li>
</ul>
<div class="section" id="working-with-manual-escaping">
<h3>Working with Manual Escaping<a class="headerlink" href="#working-with-manual-escaping" title="Permalink to this headline">¶</a></h3>
<p>If manual escaping is enabled, it’s <strong>your</strong> responsibility to escape
variables if needed. What to escape? If you have a variable that <em>may</em>
include any of the following chars (<code class="docutils literal notranslate"><span class="pre">></span></code>, <code class="docutils literal notranslate"><span class="pre"><</span></code>, <code class="docutils literal notranslate"><span class="pre">&</span></code>, or <code class="docutils literal notranslate"><span class="pre">"</span></code>) you
<strong>SHOULD</strong> escape it unless the variable contains well-formed and trusted
HTML. Escaping works by piping the variable through the <code class="docutils literal notranslate"><span class="pre">|e</span></code> filter:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span>
</pre></div>
</div>
</div>
<div class="section" id="working-with-automatic-escaping">
<h3>Working with Automatic Escaping<a class="headerlink" href="#working-with-automatic-escaping" title="Permalink to this headline">¶</a></h3>
<p>When automatic escaping is enabled, everything is escaped by default except
for values explicitly marked as safe. Variables and expressions
can be marked as safe either in:</p>
<ol class="loweralpha simple">
<li>the context dictionary by the application with <cite>MarkupSafe.Markup</cite>, or</li>
<li>the template, with the <cite>|safe</cite> filter</li>
</ol>
<p>The main problem with this approach is that Python itself doesn’t have the
concept of tainted values; so whether a value is safe or unsafe can get lost.</p>
<p>If a value is not marked safe, auto-escaping will take place; which means that
you could end up with double-escaped contents. Double-escaping is easy to
avoid, however: just rely on the tools Jinja2 provides and <em>don’t use builtin
Python constructs such as str.format or the string modulo operator (%)</em>.</p>
<p>Jinja2 functions (macros, <cite>super</cite>, <cite>self.BLOCKNAME</cite>) always return template
data that is marked as safe.</p>
<p>String literals in templates with automatic escaping are considered unsafe
because native Python strings (<code class="docutils literal notranslate"><span class="pre">str</span></code>, <code class="docutils literal notranslate"><span class="pre">unicode</span></code>, <code class="docutils literal notranslate"><span class="pre">basestring</span></code>) are not
<cite>MarkupSafe.Markup</cite> strings with an <code class="docutils literal notranslate"><span class="pre">__html__</span></code> attribute.</p>
</div>
</div>
<div class="section" id="list-of-control-structures">
<span id="id7"></span><h2>List of Control Structures<a class="headerlink" href="#list-of-control-structures" title="Permalink to this headline">¶</a></h2>
<p>A control structure refers to all those things that control the flow of a
program - conditionals (i.e. if/elif/else), for-loops, as well as things like
macros and blocks. With the default syntax, control structures appear inside
<code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">...</span> <span class="pre">%}</span></code> blocks.</p>
<div class="section" id="for">
<span id="for-loop"></span><h3>For<a class="headerlink" href="#for" title="Permalink to this headline">¶</a></h3>
<p>Loop over each item in a sequence. For example, to display a list of users
provided in a variable called <cite>users</cite>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">h1</span><span class="p">></span>Members<span class="p"></</span><span class="nt">h1</span><span class="p">></span>
<span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>As variables in templates retain their object properties, it is possible to
iterate over containers like <cite>dict</cite>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">dl</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">key</span><span class="o">,</span> <span class="nv">value</span> <span class="k">in</span> <span class="nv">my_dict.iteritems</span><span class="o">()</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">dt</span><span class="p">></span><span class="cp">{{</span> <span class="nv">key</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dt</span><span class="p">></span>
<span class="p"><</span><span class="nt">dd</span><span class="p">></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dd</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">dl</span><span class="p">></span>
</pre></div>
</div>
<p>Note, however, that <strong>Python dicts are not ordered</strong>; so you might want to
either pass a sorted <code class="docutils literal notranslate"><span class="pre">list</span></code> of <code class="docutils literal notranslate"><span class="pre">tuple</span></code> s – or a
<code class="docutils literal notranslate"><span class="pre">collections.OrderedDict</span></code> – to the template, or use the <cite>dictsort</cite> filter.</p>
<p>Inside of a for-loop block, you can access some special variables:</p>
<table border="1" class="docutils">
<colgroup>
<col width="31%" />
<col width="69%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Variable</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><cite>loop.index</cite></td>
<td>The current iteration of the loop. (1 indexed)</td>
</tr>
<tr class="row-odd"><td><cite>loop.index0</cite></td>
<td>The current iteration of the loop. (0 indexed)</td>
</tr>
<tr class="row-even"><td><cite>loop.revindex</cite></td>
<td>The number of iterations from the end of the loop
(1 indexed)</td>
</tr>
<tr class="row-odd"><td><cite>loop.revindex0</cite></td>
<td>The number of iterations from the end of the loop
(0 indexed)</td>
</tr>
<tr class="row-even"><td><cite>loop.first</cite></td>
<td>True if first iteration.</td>
</tr>
<tr class="row-odd"><td><cite>loop.last</cite></td>
<td>True if last iteration.</td>
</tr>
<tr class="row-even"><td><cite>loop.length</cite></td>
<td>The number of items in the sequence.</td>
</tr>
<tr class="row-odd"><td><cite>loop.cycle</cite></td>
<td>A helper function to cycle between a list of
sequences. See the explanation below.</td>
</tr>
<tr class="row-even"><td><cite>loop.depth</cite></td>
<td>Indicates how deep in a recursive loop
the rendering currently is. Starts at level 1</td>
</tr>
<tr class="row-odd"><td><cite>loop.depth0</cite></td>
<td>Indicates how deep in a recursive loop
the rendering currently is. Starts at level 0</td>
</tr>
<tr class="row-even"><td><cite>loop.previtem</cite></td>
<td>The item from the previous iteration of the loop.
Undefined during the first iteration.</td>
</tr>
<tr class="row-odd"><td><cite>loop.nextitem</cite></td>
<td>The item from the following iteration of the loop.
Undefined during the last iteration.</td>
</tr>
<tr class="row-even"><td><cite>loop.changed(*val)</cite></td>
<td>True if previously called with a different value
(or not called at all).</td>
</tr>
</tbody>
</table>
<p>Within a for-loop, it’s possible to cycle among a list of strings/variables
each time through the loop by using the special <cite>loop.cycle</cite> helper:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">row</span> <span class="k">in</span> <span class="nv">rows</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nb">loop</span><span class="nv">.cycle</span><span class="o">(</span><span class="s1">'odd'</span><span class="o">,</span> <span class="s1">'even'</span><span class="o">)</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">row</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Since Jinja 2.1, an extra <cite>cycle</cite> helper exists that allows loop-unbound
cycling. For more information, have a look at the <a class="reference internal" href="#builtin-globals"><span class="std std-ref">List of Global Functions</span></a>.</p>
<p id="loop-filtering">Unlike in Python, it’s not possible to <cite>break</cite> or <cite>continue</cite> in a loop. You
can, however, filter the sequence during iteration, which allows you to skip
items. The following example skips all the users which are hidden:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">user.hidden</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The advantage is that the special <cite>loop</cite> variable will count correctly; thus
not counting the users not iterated over.</p>
<p>If no iteration took place because the sequence was empty or the filtering
removed all the items from the sequence, you can render a default block
by using <cite>else</cite>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">em</span><span class="p">></span>no users found<span class="p"></</span><span class="nt">em</span><span class="p">></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>Note that, in Python, <cite>else</cite> blocks are executed whenever the corresponding
loop <strong>did not</strong> <cite>break</cite>. Since Jinja loops cannot <cite>break</cite> anyway,
a slightly different behavior of the <cite>else</cite> keyword was chosen.</p>
<p>It is also possible to use loops recursively. This is useful if you are
dealing with recursive data such as sitemaps or RDFa.
To use loops recursively, you basically have to add the <cite>recursive</cite> modifier
to the loop definition and call the <cite>loop</cite> variable with the new iterable
where you want to recurse.</p>
<p>The following example implements a sitemap with recursive loops:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span> <span class="na">class</span><span class="o">=</span><span class="s">"sitemap"</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">sitemap</span> <span class="k">recursive</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">item.href</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">item.title</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">a</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">if</span> <span class="nv">item.children</span> -<span class="cp">%}</span>
<span class="p"><</span><span class="nt">ul</span> <span class="na">class</span><span class="o">=</span><span class="s">"submenu"</span><span class="p">></span><span class="cp">{{</span> <span class="nb">loop</span><span class="o">(</span><span class="nv">item.children</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endif</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>The <cite>loop</cite> variable always refers to the closest (innermost) loop. If we
have more than one level of loops, we can rebind the variable <cite>loop</cite> by
writing <cite>{% set outer_loop = loop %}</cite> after the loop that we want to
use recursively. Then, we can call it using <cite>{{ outer_loop(…) }}</cite></p>
<p>Please note that assignments in loops will be cleared at the end of the
iteration and cannot outlive the loop scope. Older versions of Jinja2 had
a bug where in some circumstances it appeared that assignments would work.
This is not supported. See <a class="reference internal" href="#assignments"><span class="std std-ref">Assignments</span></a> for more information about
how to deal with this.</p>
<p>If all you want to do is check whether some value has changed since the
last iteration or will change in the next iteration, you can use <cite>previtem</cite>
and <cite>nextitem</cite>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">value</span> <span class="k">in</span> <span class="nv">values</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nb">loop</span><span class="nv">.previtem</span> <span class="k">is</span> <span class="nf">defined</span> <span class="k">and</span> <span class="nv">value</span> <span class="o">></span> <span class="nb">loop</span><span class="nv">.previtem</span> <span class="cp">%}</span>
The value just increased!
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">value</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nb">loop</span><span class="nv">.nextitem</span> <span class="k">is</span> <span class="nf">defined</span> <span class="k">and</span> <span class="nb">loop</span><span class="nv">.nextitem</span> <span class="o">></span> <span class="nv">value</span> <span class="cp">%}</span>
The value will increase even more!
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If you only care whether the value changed at all, using <cite>changed</cite> is even
easier:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">entry</span> <span class="k">in</span> <span class="nv">entries</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nb">loop</span><span class="nv">.changed</span><span class="o">(</span><span class="nv">entry.category</span><span class="o">)</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">h2</span><span class="p">></span><span class="cp">{{</span> <span class="nv">entry.category</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">h2</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">entry.message</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="if">
<span id="id8"></span><h3>If<a class="headerlink" href="#if" title="Permalink to this headline">¶</a></h3>
<p>The <cite>if</cite> statement in Jinja is comparable with the Python if statement.
In the simplest form, you can use it to test if a variable is defined, not
empty and not false:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>For multiple branches, <cite>elif</cite> and <cite>else</cite> can be used like in Python. You can
use more complex <a class="reference internal" href="#expressions"><span class="std std-ref">Expressions</span></a> there, too:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">kenny.sick</span> <span class="cp">%}</span>
Kenny is sick.
<span class="cp">{%</span> <span class="k">elif</span> <span class="nv">kenny.dead</span> <span class="cp">%}</span>
You killed Kenny! You bastard!!!
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
Kenny looks okay --- so far
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If can also be used as an <a class="reference internal" href="#if-expression"><span class="std std-ref">inline expression</span></a> and for
<a class="reference internal" href="#loop-filtering"><span class="std std-ref">loop filtering</span></a>.</p>
</div>
<div class="section" id="macros">
<span id="id9"></span><h3>Macros<a class="headerlink" href="#macros" title="Permalink to this headline">¶</a></h3>
<p>Macros are comparable with functions in regular programming languages. They
are useful to put often used idioms into reusable functions to not repeat
yourself (“DRY”).</p>
<p>Here’s a small example of a macro that renders a form element:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">macro</span> <span class="nv">input</span><span class="o">(</span><span class="nv">name</span><span class="o">,</span> <span class="nv">value</span><span class="o">=</span><span class="s1">''</span><span class="o">,</span> <span class="nv">type</span><span class="o">=</span><span class="s1">'text'</span><span class="o">,</span> <span class="nv">size</span><span class="o">=</span><span class="m">20</span><span class="o">)</span> -<span class="cp">%}</span>
<span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">type</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">name</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span>
<span class="nv">value</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">size</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">size</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endmacro</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The macro can then be called like a function in the namespace:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">input</span><span class="o">(</span><span class="s1">'username'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">input</span><span class="o">(</span><span class="s1">'password'</span><span class="o">,</span> <span class="nv">type</span><span class="o">=</span><span class="s1">'password'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span>
</pre></div>
</div>
<p>If the macro was defined in a different template, you have to
<a class="reference internal" href="#import"><span class="std std-ref">import</span></a> it first.</p>
<p>Inside macros, you have access to three special variables:</p>
<dl class="docutils">
<dt><cite>varargs</cite></dt>
<dd>If more positional arguments are passed to the macro than accepted by the
macro, they end up in the special <cite>varargs</cite> variable as a list of values.</dd>
<dt><cite>kwargs</cite></dt>
<dd>Like <cite>varargs</cite> but for keyword arguments. All unconsumed keyword
arguments are stored in this special variable.</dd>
<dt><cite>caller</cite></dt>
<dd>If the macro was called from a <a class="reference internal" href="#call"><span class="std std-ref">call</span></a> tag, the caller is stored
in this variable as a callable macro.</dd>
</dl>
<p>Macros also expose some of their internal details. The following attributes
are available on a macro object:</p>
<dl class="docutils">
<dt><cite>name</cite></dt>
<dd>The name of the macro. <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">input.name</span> <span class="pre">}}</span></code> will print <code class="docutils literal notranslate"><span class="pre">input</span></code>.</dd>
<dt><cite>arguments</cite></dt>
<dd>A tuple of the names of arguments the macro accepts.</dd>
<dt><cite>defaults</cite></dt>
<dd>A tuple of default values.</dd>
<dt><cite>catch_kwargs</cite></dt>
<dd>This is <cite>true</cite> if the macro accepts extra keyword arguments (i.e.: accesses
the special <cite>kwargs</cite> variable).</dd>
<dt><cite>catch_varargs</cite></dt>
<dd>This is <cite>true</cite> if the macro accepts extra positional arguments (i.e.:
accesses the special <cite>varargs</cite> variable).</dd>
<dt><cite>caller</cite></dt>
<dd>This is <cite>true</cite> if the macro accesses the special <cite>caller</cite> variable and may
be called from a <a class="reference internal" href="#call"><span class="std std-ref">call</span></a> tag.</dd>
</dl>
<p>If a macro name starts with an underscore, it’s not exported and can’t
be imported.</p>
</div>
<div class="section" id="call">
<span id="id10"></span><h3>Call<a class="headerlink" href="#call" title="Permalink to this headline">¶</a></h3>
<p>In some cases it can be useful to pass a macro to another macro. For this
purpose, you can use the special <cite>call</cite> block. The following example shows
a macro that takes advantage of the call functionality and how it can be
used:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">macro</span> <span class="nv">render_dialog</span><span class="o">(</span><span class="nv">title</span><span class="o">,</span> <span class="nv">class</span><span class="o">=</span><span class="s1">'dialog'</span><span class="o">)</span> -<span class="cp">%}</span>
<span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">class</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span>
<span class="p"><</span><span class="nt">h2</span><span class="p">></span><span class="cp">{{</span> <span class="nv">title</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">h2</span><span class="p">></span>
<span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"contents"</span><span class="p">></span>
<span class="cp">{{</span> <span class="nv">caller</span><span class="o">()</span> <span class="cp">}}</span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endmacro</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">call</span> <span class="nv">render_dialog</span><span class="o">(</span><span class="s1">'Hello World'</span><span class="o">)</span> <span class="cp">%}</span>
This is a simple dialog rendered by using a macro and
a call block.
<span class="cp">{%</span> <span class="k">endcall</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>It’s also possible to pass arguments back to the call block. This makes it
useful as a replacement for loops. Generally speaking, a call block works
exactly like a macro without a name.</p>
<p>Here’s an example of how a call block can be used with arguments:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">macro</span> <span class="nv">dump_users</span><span class="o">(</span><span class="nv">users</span><span class="o">)</span> -<span class="cp">%}</span>
<span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.username</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">caller</span><span class="o">(</span><span class="nv">user</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endmacro</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">call</span><span class="o">(</span><span class="nv">user</span><span class="o">)</span> <span class="nv">dump_users</span><span class="o">(</span><span class="nv">list_of_user</span><span class="o">)</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">dl</span><span class="p">></span>
<span class="p"><</span><span class="nt">dl</span><span class="p">></span>Realname<span class="p"></</span><span class="nt">dl</span><span class="p">></span>
<span class="p"><</span><span class="nt">dd</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.realname</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dd</span><span class="p">></span>
<span class="p"><</span><span class="nt">dl</span><span class="p">></span>Description<span class="p"></</span><span class="nt">dl</span><span class="p">></span>
<span class="p"><</span><span class="nt">dd</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.description</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dd</span><span class="p">></span>
<span class="p"></</span><span class="nt">dl</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endcall</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="id11">
<h3>Filters<a class="headerlink" href="#id11" title="Permalink to this headline">¶</a></h3>
<p>Filter sections allow you to apply regular Jinja2 filters on a block of
template data. Just wrap the code in the special <cite>filter</cite> section:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">filter</span> <span class="nf">upper</span> <span class="cp">%}</span>
This text becomes uppercase
<span class="cp">{%</span> <span class="k">endfilter</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="assignments">
<span id="id12"></span><h3>Assignments<a class="headerlink" href="#assignments" title="Permalink to this headline">¶</a></h3>
<p>Inside code blocks, you can also assign values to variables. Assignments at
top level (outside of blocks, macros or loops) are exported from the template
like top level macros and can be imported by other templates.</p>
<p>Assignments use the <cite>set</cite> tag and can have multiple targets:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">navigation</span> <span class="o">=</span> <span class="o">[(</span><span class="s1">'index.html'</span><span class="o">,</span> <span class="s1">'Index'</span><span class="o">),</span> <span class="o">(</span><span class="s1">'about.html'</span><span class="o">,</span> <span class="s1">'About'</span><span class="o">)]</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">key</span><span class="o">,</span> <span class="nv">value</span> <span class="o">=</span> <span class="nv">call_something</span><span class="o">()</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition-scoping-behavior admonition">
<p class="first admonition-title">Scoping Behavior</p>
<p>Please keep in mind that it is not possible to set variables inside a
block and have them show up outside of it. This also applies to
loops. The only exception to that rule are if statements which do not
introduce a scope. As a result the following template is not going
to do what you might expect:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">iterated</span> <span class="o">=</span> <span class="kp">false</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">seq</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">item</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">iterated</span> <span class="o">=</span> <span class="kp">true</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="k">not</span> <span class="nv">iterated</span> <span class="cp">%}</span> did not iterate <span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>It is not possible with Jinja syntax to do this. Instead use
alternative constructs like the loop else block or the special <cite>loop</cite>
variable:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">seq</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">item</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span>
did not iterate
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>As of version 2.10 more complex use cases can be handled using namespace
objects which allow propagating of changes across scopes:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">ns</span> <span class="o">=</span> <span class="nv">namespace</span><span class="o">(</span><span class="nv">found</span><span class="o">=</span><span class="kp">false</span><span class="o">)</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">items</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">item.check_something</span><span class="o">()</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">ns.found</span> <span class="o">=</span> <span class="kp">true</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
* <span class="cp">{{</span> <span class="nv">item.title</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
Found item having something: <span class="cp">{{</span> <span class="nv">ns.found</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Note hat the <code class="docutils literal notranslate"><span class="pre">obj.attr</span></code> notation in the <cite>set</cite> tag is only allowed for
namespace objects; attempting to assign an attribute on any other object
will raise an exception.</p>
<div class="last versionadded">
<p><span class="versionmodified">New in version 2.10: </span>Added support for namespace objects</p>
</div>
</div>
</div>
<div class="section" id="block-assignments">
<h3>Block Assignments<a class="headerlink" href="#block-assignments" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.8.</span></p>
</div>
<p>Starting with Jinja 2.8, it’s possible to also use block assignments to
capture the contents of a block into a variable name. This can be useful
in some situations as an alternative for macros. In that case, instead of
using an equals sign and a value, you just write the variable name and then
everything until <code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">endset</span> <span class="pre">%}</span></code> is captured.</p>
<p>Example:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">navigation</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"/"</span><span class="p">></span>Index<span class="p"></</span><span class="nt">a</span><span class="p">></span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"/downloads"</span><span class="p">></span>Downloads<span class="p"></</span><span class="nt">a</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endset</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The <cite>navigation</cite> variable then contains the navigation HTML source.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.10.</span></p>
</div>
<p>Starting with Jinja 2.10, the block assignment supports filters.</p>
<p>Example:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">reply</span> <span class="o">|</span> <span class="nf">wordwrap</span> <span class="cp">%}</span>
You wrote:
<span class="cp">{{</span> <span class="nv">message</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endset</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="extends">
<span id="id13"></span><h3>Extends<a class="headerlink" href="#extends" title="Permalink to this headline">¶</a></h3>
<p>The <cite>extends</cite> tag can be used to extend one template from another. You can
have multiple <cite>extends</cite> tags in a file, but only one of them may be executed at
a time.</p>
<p>See the section about <a class="reference internal" href="#template-inheritance"><span class="std std-ref">Template Inheritance</span></a> above.</p>
</div>
<div class="section" id="blocks">
<span id="id14"></span><h3>Blocks<a class="headerlink" href="#blocks" title="Permalink to this headline">¶</a></h3>
<p>Blocks are used for inheritance and act as both placeholders and replacements
at the same time. They are documented in detail in the
<a class="reference internal" href="#template-inheritance"><span class="std std-ref">Template Inheritance</span></a> section.</p>
</div>
<div class="section" id="include">
<h3>Include<a class="headerlink" href="#include" title="Permalink to this headline">¶</a></h3>
<p>The <cite>include</cite> statement is useful to include a template and return the
rendered contents of that file into the current namespace:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s1">'header.html'</span> <span class="cp">%}</span>
Body
<span class="cp">{%</span> <span class="k">include</span> <span class="s1">'footer.html'</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Included templates have access to the variables of the active context by
default. For more details about context behavior of imports and includes,
see <a class="reference internal" href="#import-visibility"><span class="std std-ref">Import Context Behavior</span></a>.</p>
<p>From Jinja 2.2 onwards, you can mark an include with <code class="docutils literal notranslate"><span class="pre">ignore</span> <span class="pre">missing</span></code>; in
which case Jinja will ignore the statement if the template to be included
does not exist. When combined with <code class="docutils literal notranslate"><span class="pre">with</span></code> or <code class="docutils literal notranslate"><span class="pre">without</span> <span class="pre">context</span></code>, it must
be placed <em>before</em> the context visibility statement. Here are some valid
examples:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="s2">"sidebar.html"</span> <span class="k">ignore missing</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">include</span> <span class="s2">"sidebar.html"</span> <span class="k">ignore missing</span> <span class="k">with context</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">include</span> <span class="s2">"sidebar.html"</span> <span class="k">ignore missing</span> <span class="k">without context</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.2.</span></p>
</div>
<p>You can also provide a list of templates that are checked for existence
before inclusion. The first template that exists will be included. If
<cite>ignore missing</cite> is given, it will fall back to rendering nothing if
none of the templates exist, otherwise it will raise an exception.</p>
<p>Example:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">include</span> <span class="o">[</span><span class="s1">'page_detailed.html'</span><span class="o">,</span> <span class="s1">'page.html'</span><span class="o">]</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">include</span> <span class="o">[</span><span class="s1">'special_sidebar.html'</span><span class="o">,</span> <span class="s1">'sidebar.html'</span><span class="o">]</span> <span class="k">ignore missing</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>If a template object was passed to the template context, you can
include that object using <cite>include</cite>.</p>
</div>
</div>
<div class="section" id="import">
<span id="id15"></span><h3>Import<a class="headerlink" href="#import" title="Permalink to this headline">¶</a></h3>
<p>Jinja2 supports putting often used code into macros. These macros can go into
different templates and get imported from there. This works similarly to the
import statements in Python. It’s important to know that imports are cached
and imported templates don’t have access to the current template variables,
just the globals by default. For more details about context behavior of
imports and includes, see <a class="reference internal" href="#import-visibility"><span class="std std-ref">Import Context Behavior</span></a>.</p>
<p>There are two ways to import templates. You can import a complete template
into a variable or request specific macros / exported variables from it.</p>
<p>Imagine we have a helper module that renders forms (called <cite>forms.html</cite>):</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">macro</span> <span class="nv">input</span><span class="o">(</span><span class="nv">name</span><span class="o">,</span> <span class="nv">value</span><span class="o">=</span><span class="s1">''</span><span class="o">,</span> <span class="nv">type</span><span class="o">=</span><span class="s1">'text'</span><span class="o">)</span> -<span class="cp">%}</span>
<span class="p"><</span><span class="nt">input</span> <span class="na">type</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">type</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">value</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">name</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endmacro</span> <span class="cp">%}</span>
<span class="cp">{%</span>- <span class="k">macro</span> <span class="nv">textarea</span><span class="o">(</span><span class="nv">name</span><span class="o">,</span> <span class="nv">value</span><span class="o">=</span><span class="s1">''</span><span class="o">,</span> <span class="nv">rows</span><span class="o">=</span><span class="m">10</span><span class="o">,</span> <span class="nv">cols</span><span class="o">=</span><span class="m">40</span><span class="o">)</span> -<span class="cp">%}</span>
<span class="p"><</span><span class="nt">textarea</span> <span class="na">name</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">rows</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">rows</span> <span class="cp">}}</span><span class="s">"</span> <span class="na">cols</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">cols</span>
<span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">value</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">textarea</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endmacro</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The easiest and most flexible way to access a template’s variables
and macros is to import the whole template module into a variable.
That way, you can access the attributes:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">import</span> <span class="s1">'forms.html'</span> <span class="k">as</span> <span class="nv">forms</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">dl</span><span class="p">></span>
<span class="p"><</span><span class="nt">dt</span><span class="p">></span>Username<span class="p"></</span><span class="nt">dt</span><span class="p">></span>
<span class="p"><</span><span class="nt">dd</span><span class="p">></span><span class="cp">{{</span> <span class="nv">forms.input</span><span class="o">(</span><span class="s1">'username'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dd</span><span class="p">></span>
<span class="p"><</span><span class="nt">dt</span><span class="p">></span>Password<span class="p"></</span><span class="nt">dt</span><span class="p">></span>
<span class="p"><</span><span class="nt">dd</span><span class="p">></span><span class="cp">{{</span> <span class="nv">forms.input</span><span class="o">(</span><span class="s1">'password'</span><span class="o">,</span> <span class="nv">type</span><span class="o">=</span><span class="s1">'password'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dd</span><span class="p">></span>
<span class="p"></</span><span class="nt">dl</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">forms.textarea</span><span class="o">(</span><span class="s1">'comment'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span>
</pre></div>
</div>
<p>Alternatively, you can import specific names from a template into the current
namespace:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">from</span> <span class="s1">'forms.html'</span> <span class="k">import</span> <span class="nv">input</span> <span class="k">as</span> <span class="nv">input_field</span><span class="o">,</span> <span class="nv">textarea</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">dl</span><span class="p">></span>
<span class="p"><</span><span class="nt">dt</span><span class="p">></span>Username<span class="p"></</span><span class="nt">dt</span><span class="p">></span>
<span class="p"><</span><span class="nt">dd</span><span class="p">></span><span class="cp">{{</span> <span class="nv">input_field</span><span class="o">(</span><span class="s1">'username'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dd</span><span class="p">></span>
<span class="p"><</span><span class="nt">dt</span><span class="p">></span>Password<span class="p"></</span><span class="nt">dt</span><span class="p">></span>
<span class="p"><</span><span class="nt">dd</span><span class="p">></span><span class="cp">{{</span> <span class="nv">input_field</span><span class="o">(</span><span class="s1">'password'</span><span class="o">,</span> <span class="nv">type</span><span class="o">=</span><span class="s1">'password'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">dd</span><span class="p">></span>
<span class="p"></</span><span class="nt">dl</span><span class="p">></span>
<span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{{</span> <span class="nv">textarea</span><span class="o">(</span><span class="s1">'comment'</span><span class="o">)</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span>
</pre></div>
</div>
<p>Macros and variables starting with one or more underscores are private and
cannot be imported.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.4: </span>If a template object was passed to the template context, you can
import from that object.</p>
</div>
</div>
</div>
<div class="section" id="import-context-behavior">
<span id="import-visibility"></span><h2>Import Context Behavior<a class="headerlink" href="#import-context-behavior" title="Permalink to this headline">¶</a></h2>
<p>By default, included templates are passed the current context and imported
templates are not. The reason for this is that imports, unlike includes,
are cached; as imports are often used just as a module that holds macros.</p>
<p>This behavior can be changed explicitly: by adding <cite>with context</cite>
or <cite>without context</cite> to the import/include directive, the current context
can be passed to the template and caching is disabled automatically.</p>
<p>Here are two examples:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">from</span> <span class="s1">'forms.html'</span> <span class="k">import</span> <span class="nv">input</span> <span class="k">with context</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">include</span> <span class="s1">'header.html'</span> <span class="k">without context</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p>In Jinja 2.0, the context that was passed to the included template
did not include variables defined in the template. As a matter of
fact, this did not work:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">box</span> <span class="k">in</span> <span class="nv">boxes</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">include</span> <span class="s2">"render_box.html"</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p class="last">The included template <code class="docutils literal notranslate"><span class="pre">render_box.html</span></code> is <em>not</em> able to access
<cite>box</cite> in Jinja 2.0. As of Jinja 2.1, <code class="docutils literal notranslate"><span class="pre">render_box.html</span></code> <em>is</em> able
to do so.</p>
</div>
</div>
<div class="section" id="expressions">
<span id="id16"></span><h2>Expressions<a class="headerlink" href="#expressions" title="Permalink to this headline">¶</a></h2>
<p>Jinja allows basic expressions everywhere. These work very similarly to
regular Python; even if you’re not working with Python
you should feel comfortable with it.</p>
<div class="section" id="literals">
<h3>Literals<a class="headerlink" href="#literals" title="Permalink to this headline">¶</a></h3>
<p>The simplest form of expressions are literals. Literals are representations
for Python objects such as strings and numbers. The following literals exist:</p>
<dl class="docutils">
<dt>“Hello World”:</dt>
<dd>Everything between two double or single quotes is a string. They are
useful whenever you need a string in the template (e.g. as
arguments to function calls and filters, or just to extend or include a
template).</dd>
<dt>42 / 42.23:</dt>
<dd>Integers and floating point numbers are created by just writing the
number down. If a dot is present, the number is a float, otherwise an
integer. Keep in mind that, in Python, <code class="docutils literal notranslate"><span class="pre">42</span></code> and <code class="docutils literal notranslate"><span class="pre">42.0</span></code>
are different (<code class="docutils literal notranslate"><span class="pre">int</span></code> and <code class="docutils literal notranslate"><span class="pre">float</span></code>, respectively).</dd>
<dt>[‘list’, ‘of’, ‘objects’]:</dt>
<dd><p class="first">Everything between two brackets is a list. Lists are useful for storing
sequential data to be iterated over. For example, you can easily
create a list of links using lists and tuples for (and with) a for loop:</p>
<div class="last highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">href</span><span class="o">,</span> <span class="nv">caption</span> <span class="k">in</span> <span class="o">[(</span><span class="s1">'index.html'</span><span class="o">,</span> <span class="s1">'Index'</span><span class="o">),</span> <span class="o">(</span><span class="s1">'about.html'</span><span class="o">,</span> <span class="s1">'About'</span><span class="o">),</span>
<span class="o">(</span><span class="s1">'downloads.html'</span><span class="o">,</span> <span class="s1">'Downloads'</span><span class="o">)]</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"</span><span class="cp">{{</span> <span class="nv">href</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">caption</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">a</span><span class="p">></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
</dd>
<dt>(‘tuple’, ‘of’, ‘values’):</dt>
<dd>Tuples are like lists that cannot be modified (“immutable”). If a tuple
only has one item, it must be followed by a comma (<code class="docutils literal notranslate"><span class="pre">('1-tuple',)</span></code>).
Tuples are usually used to represent items of two or more elements.
See the list example above for more details.</dd>
<dt>{‘dict’: ‘of’, ‘key’: ‘and’, ‘value’: ‘pairs’}:</dt>
<dd>A dict in Python is a structure that combines keys and values. Keys must
be unique and always have exactly one value. Dicts are rarely used in
templates; they are useful in some rare cases such as the <a class="reference internal" href="#xmlattr" title="xmlattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">xmlattr()</span></code></a>
filter.</dd>
<dt>true / false:</dt>
<dd>true is always true and false is always false.</dd>
</dl>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">The special constants <cite>true</cite>, <cite>false</cite>, and <cite>none</cite> are indeed lowercase.
Because that caused confusion in the past, (<cite>True</cite> used to expand
to an undefined variable that was considered false),
all three can now also be written in title case
(<cite>True</cite>, <cite>False</cite>, and <cite>None</cite>).
However, for consistency, (all Jinja identifiers are lowercase)
you should use the lowercase versions.</p>
</div>
</div>
<div class="section" id="math">
<h3>Math<a class="headerlink" href="#math" title="Permalink to this headline">¶</a></h3>
<p>Jinja allows you to calculate with values. This is rarely useful in templates
but exists for completeness’ sake. The following operators are supported:</p>
<dl class="docutils">
<dt>+</dt>
<dd>Adds two objects together. Usually the objects are numbers, but if both are
strings or lists, you can concatenate them this way. This, however, is not
the preferred way to concatenate strings! For string concatenation, have
a look-see at the <code class="docutils literal notranslate"><span class="pre">~</span></code> operator. <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">1</span> <span class="pre">+</span> <span class="pre">1</span> <span class="pre">}}</span></code> is <code class="docutils literal notranslate"><span class="pre">2</span></code>.</dd>
<dt>-</dt>
<dd>Subtract the second number from the first one. <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">3</span> <span class="pre">-</span> <span class="pre">2</span> <span class="pre">}}</span></code> is <code class="docutils literal notranslate"><span class="pre">1</span></code>.</dd>
</dl>
<dl class="docutils">
<dt>/</dt>
<dd>Divide two numbers. The return value will be a floating point number.
<code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">1</span> <span class="pre">/</span> <span class="pre">2</span> <span class="pre">}}</span></code> is <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">0.5</span> <span class="pre">}}</span></code>.
(Just like <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">division</span></code>.)</dd>
</dl>
<dl class="docutils">
<dt>//</dt>
<dd>Divide two numbers and return the truncated integer result.
<code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">20</span> <span class="pre">//</span> <span class="pre">7</span> <span class="pre">}}</span></code> is <code class="docutils literal notranslate"><span class="pre">2</span></code>.</dd>
</dl>
<dl class="docutils">
<dt>%</dt>
<dd>Calculate the remainder of an integer division. <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">11</span> <span class="pre">%</span> <span class="pre">7</span> <span class="pre">}}</span></code> is <code class="docutils literal notranslate"><span class="pre">4</span></code>.</dd>
<dt>*</dt>
<dd>Multiply the left operand with the right one. <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">2</span> <span class="pre">*</span> <span class="pre">2</span> <span class="pre">}}</span></code> would
return <code class="docutils literal notranslate"><span class="pre">4</span></code>. This can also be used to repeat a string multiple times.
<code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">'='</span> <span class="pre">*</span> <span class="pre">80</span> <span class="pre">}}</span></code> would print a bar of 80 equal signs.</dd>
<dt>**</dt>
<dd>Raise the left operand to the power of the right operand. <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">2**3</span> <span class="pre">}}</span></code>
would return <code class="docutils literal notranslate"><span class="pre">8</span></code>.</dd>
</dl>
</div>
<div class="section" id="comparisons">
<h3>Comparisons<a class="headerlink" href="#comparisons" title="Permalink to this headline">¶</a></h3>
<dl class="docutils">
<dt>==</dt>
<dd>Compares two objects for equality.</dd>
<dt>!=</dt>
<dd>Compares two objects for inequality.</dd>
</dl>
<dl class="docutils">
<dt>></dt>
<dd><cite>true</cite> if the left hand side is greater than the right hand side.</dd>
<dt>>=</dt>
<dd><cite>true</cite> if the left hand side is greater or equal to the right hand side.</dd>
</dl>
<dl class="docutils">
<dt><</dt>
<dd><cite>true</cite> if the left hand side is lower than the right hand side.</dd>
<dt><=</dt>
<dd><cite>true</cite> if the left hand side is lower or equal to the right hand side.</dd>
</dl>
</div>
<div class="section" id="logic">
<h3>Logic<a class="headerlink" href="#logic" title="Permalink to this headline">¶</a></h3>
<p>For <cite>if</cite> statements, <cite>for</cite> filtering, and <cite>if</cite> expressions, it can be useful to
combine multiple expressions:</p>
<dl class="docutils">
<dt>and</dt>
<dd>Return true if the left and the right operand are true.</dd>
<dt>or</dt>
<dd>Return true if the left or the right operand are true.</dd>
<dt>not</dt>
<dd>negate a statement (see below).</dd>
<dt>(expr)</dt>
<dd>group an expression.</dd>
</dl>
<div class="admonition-note admonition">
<p class="first admonition-title">Note</p>
<p class="last">The <code class="docutils literal notranslate"><span class="pre">is</span></code> and <code class="docutils literal notranslate"><span class="pre">in</span></code> operators support negation using an infix notation,
too: <code class="docutils literal notranslate"><span class="pre">foo</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">bar</span></code> and <code class="docutils literal notranslate"><span class="pre">foo</span> <span class="pre">not</span> <span class="pre">in</span> <span class="pre">bar</span></code> instead of <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">foo</span> <span class="pre">is</span> <span class="pre">bar</span></code>
and <code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">foo</span> <span class="pre">in</span> <span class="pre">bar</span></code>. All other expressions require a prefix notation:
<code class="docutils literal notranslate"><span class="pre">not</span> <span class="pre">(foo</span> <span class="pre">and</span> <span class="pre">bar).</span></code></p>
</div>
</div>
<div class="section" id="other-operators">
<h3>Other Operators<a class="headerlink" href="#other-operators" title="Permalink to this headline">¶</a></h3>
<p>The following operators are very useful but don’t fit into any of the other
two categories:</p>
<dl class="docutils">
<dt>in</dt>
<dd>Perform a sequence / mapping containment test. Returns true if the left
operand is contained in the right. <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">1</span> <span class="pre">in</span> <span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span> <span class="pre">}}</span></code> would, for
example, return true.</dd>
<dt>is</dt>
<dd>Performs a <a class="reference internal" href="#tests"><span class="std std-ref">test</span></a>.</dd>
<dt>|</dt>
<dd>Applies a <a class="reference internal" href="#filters"><span class="std std-ref">filter</span></a>.</dd>
</dl>
<dl class="docutils">
<dt>~</dt>
<dd><p class="first">Converts all operands into strings and concatenates them.</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">"Hello</span> <span class="pre">"</span> <span class="pre">~</span> <span class="pre">name</span> <span class="pre">~</span> <span class="pre">"!"</span> <span class="pre">}}</span></code> would return (assuming <cite>name</cite> is set
to <code class="docutils literal notranslate"><span class="pre">'John'</span></code>) <code class="docutils literal notranslate"><span class="pre">Hello</span> <span class="pre">John!</span></code>.</p>
</dd>
<dt>()</dt>
<dd><p class="first">Call a callable: <code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">post.render()</span> <span class="pre">}}</span></code>. Inside of the parentheses you
can use positional arguments and keyword arguments like in Python:</p>
<p class="last"><code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">post.render(user,</span> <span class="pre">full=true)</span> <span class="pre">}}</span></code>.</p>
</dd>
<dt>. / []</dt>
<dd>Get an attribute of an object. (See <a class="reference internal" href="#variables"><span class="std std-ref">Variables</span></a>)</dd>
</dl>
</div>
<div class="section" id="if-expression">
<span id="id17"></span><h3>If Expression<a class="headerlink" href="#if-expression" title="Permalink to this headline">¶</a></h3>
<p>It is also possible to use inline <cite>if</cite> expressions. These are useful in some
situations. For example, you can use this to extend from one template if a
variable is defined, otherwise from the default layout template:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">extends</span> <span class="nv">layout_template</span> <span class="k">if</span> <span class="nv">layout_template</span> <span class="k">is</span> <span class="nf">defined</span> <span class="k">else</span> <span class="s1">'master.html'</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The general syntax is <code class="docutils literal notranslate"><span class="pre"><do</span> <span class="pre">something></span> <span class="pre">if</span> <span class="pre"><something</span> <span class="pre">is</span> <span class="pre">true></span> <span class="pre">else</span> <span class="pre"><do</span>
<span class="pre">something</span> <span class="pre">else></span></code>.</p>
<p>The <cite>else</cite> part is optional. If not provided, the else block implicitly
evaluates into an undefined object:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span>.. sourcecode:: jinja
</pre></div>
</div>
<blockquote>
<div>{{ ‘[%s]’ % page.title if page.title }}</div></blockquote>
</div>
</div>
<div class="section" id="list-of-builtin-filters">
<span id="builtin-filters"></span><h2>List of Builtin Filters<a class="headerlink" href="#list-of-builtin-filters" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="abs">
<code class="descname">abs</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#abs" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the absolute value of the argument.</p>
</dd></dl>
<dl class="function">
<dt id="attr">
<code class="descname">attr</code><span class="sig-paren">(</span><em>obj</em>, <em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#attr" title="Permalink to this definition">¶</a></dt>
<dd><p>Get an attribute of an object. <code class="docutils literal notranslate"><span class="pre">foo|attr("bar")</span></code> works like
<code class="docutils literal notranslate"><span class="pre">foo.bar</span></code> just that always an attribute is returned and items are not
looked up.</p>
<p>See <a class="reference internal" href="#notes-on-subscriptions"><span class="std std-ref">Notes on subscriptions</span></a> for more details.</p>
</dd></dl>
<dl class="function">
<dt id="batch">
<code class="descname">batch</code><span class="sig-paren">(</span><em>value</em>, <em>linecount</em>, <em>fill_with=None</em><span class="sig-paren">)</span><a class="headerlink" href="#batch" title="Permalink to this definition">¶</a></dt>
<dd><p>A filter that batches items. It works pretty much like <cite>slice</cite>
just the other way round. It returns a list of lists with the
given number of items. If you provide a second parameter this
is used to fill up missing items. See this example:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">table</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">for</span> <span class="nv">row</span> <span class="k">in</span> <span class="nv">items</span><span class="o">|</span><span class="nf">batch</span><span class="o">(</span><span class="m">3</span><span class="o">,</span> <span class="s1">'&nbsp;'</span><span class="o">)</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">tr</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">for</span> <span class="nv">column</span> <span class="k">in</span> <span class="nv">row</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">td</span><span class="p">></span><span class="cp">{{</span> <span class="nv">column</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">td</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">tr</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">table</span><span class="p">></span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="capitalize">
<code class="descname">capitalize</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#capitalize" title="Permalink to this definition">¶</a></dt>
<dd><p>Capitalize a value. The first character will be uppercase, all others
lowercase.</p>
</dd></dl>
<dl class="function">
<dt id="center">
<code class="descname">center</code><span class="sig-paren">(</span><em>value</em>, <em>width=80</em><span class="sig-paren">)</span><a class="headerlink" href="#center" title="Permalink to this definition">¶</a></dt>
<dd><p>Centers the value in a field of a given width.</p>
</dd></dl>
<dl class="function">
<dt id="default">
<code class="descname">default</code><span class="sig-paren">(</span><em>value</em>, <em>default_value=''</em>, <em>boolean=False</em><span class="sig-paren">)</span><a class="headerlink" href="#default" title="Permalink to this definition">¶</a></dt>
<dd><p>If the value is undefined it will return the passed default value,
otherwise the value of the variable:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">my_variable</span><span class="o">|</span><span class="nf">default</span><span class="o">(</span><span class="s1">'my_variable is not defined'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>This will output the value of <code class="docutils literal notranslate"><span class="pre">my_variable</span></code> if the variable was
defined, otherwise <code class="docutils literal notranslate"><span class="pre">'my_variable</span> <span class="pre">is</span> <span class="pre">not</span> <span class="pre">defined'</span></code>. If you want
to use default with variables that evaluate to false you have to
set the second parameter to <cite>true</cite>:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s1">''</span><span class="o">|</span><span class="nf">default</span><span class="o">(</span><span class="s1">'the string was empty'</span><span class="o">,</span> <span class="kp">true</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre">d</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="dictsort">
<code class="descname">dictsort</code><span class="sig-paren">(</span><em>value</em>, <em>case_sensitive=False</em>, <em>by='key'</em>, <em>reverse=False</em><span class="sig-paren">)</span><a class="headerlink" href="#dictsort" title="Permalink to this definition">¶</a></dt>
<dd><p>Sort a dict and yield (key, value) pairs. Because python dicts are
unsorted you may want to use this function to order them by either
key or value:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">mydict</span><span class="o">|</span><span class="nf">dictsort</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> sort the dict by key, case insensitive</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">mydict</span><span class="o">|</span><span class="nf">dictsort</span><span class="o">(</span><span class="nv">reverse</span><span class="o">=</span><span class="kp">true</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> sort the dict by key, case insensitive, reverse order</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">mydict</span><span class="o">|</span><span class="nf">dictsort</span><span class="o">(</span><span class="kp">true</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> sort the dict by key, case sensitive</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">mydict</span><span class="o">|</span><span class="nf">dictsort</span><span class="o">(</span><span class="kp">false</span><span class="o">,</span> <span class="s1">'value'</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> sort the dict by value, case insensitive</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="escape">
<code class="descname">escape</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#escape" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert the characters &, <, >, ‘, and ” in string s to HTML-safe
sequences. Use this if you need to display text that might contain
such characters in HTML. Marks return value as markup string.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre">e</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="filesizeformat">
<code class="descname">filesizeformat</code><span class="sig-paren">(</span><em>value</em>, <em>binary=False</em><span class="sig-paren">)</span><a class="headerlink" href="#filesizeformat" title="Permalink to this definition">¶</a></dt>
<dd><p>Format the value like a ‘human-readable’ file size (i.e. 13 kB,
4.1 MB, 102 Bytes, etc). Per default decimal prefixes are used (Mega,
Giga, etc.), if the second parameter is set to <cite>True</cite> the binary
prefixes are used (Mebi, Gibi).</p>
</dd></dl>
<dl class="function">
<dt id="first">
<code class="descname">first</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#first" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the first item of a sequence.</p>
</dd></dl>
<dl class="function">
<dt id="float">
<code class="descname">float</code><span class="sig-paren">(</span><em>value</em>, <em>default=0.0</em><span class="sig-paren">)</span><a class="headerlink" href="#float" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert the value into a floating point number. If the
conversion doesn’t work it will return <code class="docutils literal notranslate"><span class="pre">0.0</span></code>. You can
override this default using the first parameter.</p>
</dd></dl>
<dl class="function">
<dt id="forceescape">
<code class="descname">forceescape</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#forceescape" title="Permalink to this definition">¶</a></dt>
<dd><p>Enforce HTML escaping. This will probably double escape variables.</p>
</dd></dl>
<dl class="function">
<dt id="format">
<code class="descname">format</code><span class="sig-paren">(</span><em>value</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#format" title="Permalink to this definition">¶</a></dt>
<dd><p>Apply python string formatting on an object:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s2">"%s - %s"</span><span class="o">|</span><span class="nf">format</span><span class="o">(</span><span class="s2">"Hello?"</span><span class="o">,</span> <span class="s2">"Foo!"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> Hello? - Foo!</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="groupby">
<code class="descname">groupby</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#groupby" title="Permalink to this definition">¶</a></dt>
<dd><p>Group a sequence of objects by a common attribute.</p>
<p>If you for example have a list of dicts or objects that represent persons
with <cite>gender</cite>, <cite>first_name</cite> and <cite>last_name</cite> attributes and you want to
group all users by genders you can do something like the following
snippet:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">group</span> <span class="k">in</span> <span class="nv">persons</span><span class="o">|</span><span class="nf">groupby</span><span class="o">(</span><span class="s1">'gender'</span><span class="o">)</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">group.grouper</span> <span class="cp">}}</span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">person</span> <span class="k">in</span> <span class="nv">group.list</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">person.first_name</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">person.last_name</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">ul</span><span class="p">></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>Additionally it’s possible to use tuple unpacking for the grouper and
list:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">grouper</span><span class="o">,</span> <span class="nv">list</span> <span class="k">in</span> <span class="nv">persons</span><span class="o">|</span><span class="nf">groupby</span><span class="o">(</span><span class="s1">'gender'</span><span class="o">)</span> <span class="cp">%}</span>
...
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>As you can see the item we’re grouping by is stored in the <cite>grouper</cite>
attribute and the <cite>list</cite> contains all the objects that have this grouper
in common.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.6: </span>It’s now possible to use dotted notation to group by the child
attribute of another attribute.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="indent">
<code class="descname">indent</code><span class="sig-paren">(</span><em>s</em>, <em>width=4</em>, <em>first=False</em>, <em>blank=False</em>, <em>indentfirst=None</em><span class="sig-paren">)</span><a class="headerlink" href="#indent" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the string with each line indented by 4 spaces. The
first line and blank lines are not indented by default.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>width</strong> – Number of spaces to indent by.</li>
<li><strong>first</strong> – Don’t skip indenting the first line.</li>
<li><strong>blank</strong> – Don’t skip indenting empty lines.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.10: </span>Blank lines are not indented by default.</p>
<p>Rename the <code class="docutils literal notranslate"><span class="pre">indentfirst</span></code> argument to <code class="docutils literal notranslate"><span class="pre">first</span></code>.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="int">
<code class="descname">int</code><span class="sig-paren">(</span><em>value</em>, <em>default=0</em>, <em>base=10</em><span class="sig-paren">)</span><a class="headerlink" href="#int" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert the value into an integer. If the
conversion doesn’t work it will return <code class="docutils literal notranslate"><span class="pre">0</span></code>. You can
override this default using the first parameter. You
can also override the default base (10) in the second
parameter, which handles input with prefixes such as
0b, 0o and 0x for bases 2, 8 and 16 respectively.
The base is ignored for decimal numbers and non-string values.</p>
</dd></dl>
<dl class="function">
<dt id="join">
<code class="descname">join</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#join" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string which is the concatenation of the strings in the
sequence. The separator between elements is an empty string per
default, you can define it with the optional parameter:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="m">3</span><span class="o">]|</span><span class="nf">join</span><span class="o">(</span><span class="s1">'|'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> 1|2|3</span>
<span class="cp">{{</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="m">3</span><span class="o">]|</span><span class="nf">join</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> 123</span>
</pre></div>
</div>
<p>It is also possible to join certain attributes of an object:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">users</span><span class="o">|</span><span class="nf">join</span><span class="o">(</span><span class="s1">', '</span><span class="o">,</span> <span class="nv">attribute</span><span class="o">=</span><span class="s1">'username'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.6: </span>The <cite>attribute</cite> parameter was added.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="last">
<code class="descname">last</code><span class="sig-paren">(</span><em>seq</em><span class="sig-paren">)</span><a class="headerlink" href="#last" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the last item of a sequence.</p>
</dd></dl>
<dl class="function">
<dt id="length">
<code class="descname">length</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#length" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the number of items in a container.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre">count</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="list">
<code class="descname">list</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#list" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert the value into a list. If it was a string the returned list
will be a list of characters.</p>
</dd></dl>
<dl class="function">
<dt id="lower">
<code class="descname">lower</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#lower" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a value to lowercase.</p>
</dd></dl>
<dl class="function">
<dt id="map">
<code class="descname">map</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#map" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies a filter on a sequence of objects or looks up an attribute.
This is useful when dealing with lists of objects but you are really
only interested in a certain value of it.</p>
<p>The basic usage is mapping on an attribute. Imagine you have a list
of users but you are only interested in a list of usernames:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="x">Users on this page: </span><span class="cp">{{</span> <span class="nv">users</span><span class="o">|</span><span class="nf">map</span><span class="o">(</span><span class="nv">attribute</span><span class="o">=</span><span class="s1">'username'</span><span class="o">)|</span><span class="nf">join</span><span class="o">(</span><span class="s1">', '</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<p>Alternatively you can let it invoke a filter by passing the name of the
filter and the arguments afterwards. A good example would be applying a
text conversion filter on a sequence:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="x">Users on this page: </span><span class="cp">{{</span> <span class="nv">titles</span><span class="o">|</span><span class="nf">map</span><span class="o">(</span><span class="s1">'lower'</span><span class="o">)|</span><span class="nf">join</span><span class="o">(</span><span class="s1">', '</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="max">
<code class="descname">max</code><span class="sig-paren">(</span><em>value</em>, <em>case_sensitive=False</em>, <em>attribute=None</em><span class="sig-paren">)</span><a class="headerlink" href="#max" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the largest item from the sequence.</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="m">3</span><span class="o">]|</span><span class="nf">max</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> 3</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>case_sensitive</strong> – Treat upper and lower case strings as distinct.</li>
<li><strong>attribute</strong> – Get the object with the max value of this attribute.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="min">
<code class="descname">min</code><span class="sig-paren">(</span><em>value</em>, <em>case_sensitive=False</em>, <em>attribute=None</em><span class="sig-paren">)</span><a class="headerlink" href="#min" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the smallest item from the sequence.</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="o">[</span><span class="m">1</span><span class="o">,</span> <span class="m">2</span><span class="o">,</span> <span class="m">3</span><span class="o">]|</span><span class="nf">min</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> 1</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>case_sensitive</strong> – Treat upper and lower case strings as distinct.</li>
<li><strong>attribute</strong> – Get the object with the max value of this attribute.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="pprint">
<code class="descname">pprint</code><span class="sig-paren">(</span><em>value</em>, <em>verbose=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pprint" title="Permalink to this definition">¶</a></dt>
<dd><p>Pretty print a variable. Useful for debugging.</p>
<p>With Jinja 1.2 onwards you can pass it a parameter. If this parameter
is truthy the output will be more verbose (this requires <cite>pretty</cite>)</p>
</dd></dl>
<dl class="function">
<dt id="random">
<code class="descname">random</code><span class="sig-paren">(</span><em>seq</em><span class="sig-paren">)</span><a class="headerlink" href="#random" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a random item from the sequence.</p>
</dd></dl>
<dl class="function">
<dt id="reject">
<code class="descname">reject</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#reject" title="Permalink to this definition">¶</a></dt>
<dd><p>Filters a sequence of objects by applying a test to each object,
and rejecting the objects with the test succeeding.</p>
<p>If no test is specified, each object will be evaluated as a boolean.</p>
<p>Example usage:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">numbers</span><span class="o">|</span><span class="nf">reject</span><span class="o">(</span><span class="s2">"odd"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="rejectattr">
<code class="descname">rejectattr</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#rejectattr" title="Permalink to this definition">¶</a></dt>
<dd><p>Filters a sequence of objects by applying a test to the specified
attribute of each object, and rejecting the objects with the test
succeeding.</p>
<p>If no test is specified, the attribute’s value will be evaluated as
a boolean.</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">users</span><span class="o">|</span><span class="nf">rejectattr</span><span class="o">(</span><span class="s2">"is_active"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">users</span><span class="o">|</span><span class="nf">rejectattr</span><span class="o">(</span><span class="s2">"email"</span><span class="o">,</span> <span class="s2">"none"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="replace">
<code class="descname">replace</code><span class="sig-paren">(</span><em>s</em>, <em>old</em>, <em>new</em>, <em>count=None</em><span class="sig-paren">)</span><a class="headerlink" href="#replace" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the value with all occurrences of a substring
replaced with a new one. The first argument is the substring
that should be replaced, the second is the replacement string.
If the optional third argument <code class="docutils literal notranslate"><span class="pre">count</span></code> is given, only the first
<code class="docutils literal notranslate"><span class="pre">count</span></code> occurrences are replaced:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s2">"Hello World"</span><span class="o">|</span><span class="nf">replace</span><span class="o">(</span><span class="s2">"Hello"</span><span class="o">,</span> <span class="s2">"Goodbye"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> Goodbye World</span>
<span class="cp">{{</span> <span class="s2">"aaaaargh"</span><span class="o">|</span><span class="nf">replace</span><span class="o">(</span><span class="s2">"a"</span><span class="o">,</span> <span class="s2">"d'oh, "</span><span class="o">,</span> <span class="m">2</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> d'oh, d'oh, aaargh</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="reverse">
<code class="descname">reverse</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#reverse" title="Permalink to this definition">¶</a></dt>
<dd><p>Reverse the object or return an iterator that iterates over it the other
way round.</p>
</dd></dl>
<dl class="function">
<dt id="round">
<code class="descname">round</code><span class="sig-paren">(</span><em>value</em>, <em>precision=0</em>, <em>method='common'</em><span class="sig-paren">)</span><a class="headerlink" href="#round" title="Permalink to this definition">¶</a></dt>
<dd><p>Round the number to a given precision. The first
parameter specifies the precision (default is <code class="docutils literal notranslate"><span class="pre">0</span></code>), the
second the rounding method:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">'common'</span></code> rounds either up or down</li>
<li><code class="docutils literal notranslate"><span class="pre">'ceil'</span></code> always rounds up</li>
<li><code class="docutils literal notranslate"><span class="pre">'floor'</span></code> always rounds down</li>
</ul>
<p>If you don’t specify a method <code class="docutils literal notranslate"><span class="pre">'common'</span></code> is used.</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="m">42.55</span><span class="o">|</span><span class="nf">round</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> 43.0</span>
<span class="cp">{{</span> <span class="m">42.55</span><span class="o">|</span><span class="nf">round</span><span class="o">(</span><span class="m">1</span><span class="o">,</span> <span class="s1">'floor'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> 42.5</span>
</pre></div>
</div>
<p>Note that even if rounded to 0 precision, a float is returned. If
you need a real integer, pipe it through <cite>int</cite>:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="m">42.55</span><span class="o">|</span><span class="nf">round</span><span class="o">|</span><span class="nf">int</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> 43</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="safe">
<code class="descname">safe</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#safe" title="Permalink to this definition">¶</a></dt>
<dd><p>Mark the value as safe which means that in an environment with automatic
escaping enabled this variable will not be escaped.</p>
</dd></dl>
<dl class="function">
<dt id="select">
<code class="descname">select</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#select" title="Permalink to this definition">¶</a></dt>
<dd><p>Filters a sequence of objects by applying a test to each object,
and only selecting the objects with the test succeeding.</p>
<p>If no test is specified, each object will be evaluated as a boolean.</p>
<p>Example usage:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">numbers</span><span class="o">|</span><span class="nf">select</span><span class="o">(</span><span class="s2">"odd"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">numbers</span><span class="o">|</span><span class="nf">select</span><span class="o">(</span><span class="s2">"odd"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">numbers</span><span class="o">|</span><span class="nf">select</span><span class="o">(</span><span class="s2">"divisibleby"</span><span class="o">,</span> <span class="m">3</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">numbers</span><span class="o">|</span><span class="nf">select</span><span class="o">(</span><span class="s2">"lessthan"</span><span class="o">,</span> <span class="m">42</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">strings</span><span class="o">|</span><span class="nf">select</span><span class="o">(</span><span class="s2">"equalto"</span><span class="o">,</span> <span class="s2">"mystring"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="selectattr">
<code class="descname">selectattr</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#selectattr" title="Permalink to this definition">¶</a></dt>
<dd><p>Filters a sequence of objects by applying a test to the specified
attribute of each object, and only selecting the objects with the
test succeeding.</p>
<p>If no test is specified, the attribute’s value will be evaluated as
a boolean.</p>
<p>Example usage:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">users</span><span class="o">|</span><span class="nf">selectattr</span><span class="o">(</span><span class="s2">"is_active"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{{</span> <span class="nv">users</span><span class="o">|</span><span class="nf">selectattr</span><span class="o">(</span><span class="s2">"email"</span><span class="o">,</span> <span class="s2">"none"</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="slice">
<code class="descname">slice</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#slice" title="Permalink to this definition">¶</a></dt>
<dd><p>Slice an iterator and return a list of lists containing
those items. Useful if you want to create a div containing
three ul tags that represent columns:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">div</span> <span class="na">class</span><span class="o">=</span><span class="s">"columwrapper"</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">for</span> <span class="nv">column</span> <span class="k">in</span> <span class="nv">items</span><span class="o">|</span><span class="nf">slice</span><span class="o">(</span><span class="m">3</span><span class="o">)</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">ul</span> <span class="na">class</span><span class="o">=</span><span class="s">"column-</span><span class="cp">{{</span> <span class="nb">loop</span><span class="nv">.index</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">column</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">item</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">div</span><span class="p">></span>
</pre></div>
</div>
<p>If you pass it a second argument it’s used to fill missing
values on the last iteration.</p>
</dd></dl>
<dl class="function">
<dt id="sort">
<code class="descname">sort</code><span class="sig-paren">(</span><em>value</em>, <em>reverse=False</em>, <em>case_sensitive=False</em>, <em>attribute=None</em><span class="sig-paren">)</span><a class="headerlink" href="#sort" title="Permalink to this definition">¶</a></dt>
<dd><p>Sort an iterable. Per default it sorts ascending, if you pass it
true as first argument it will reverse the sorting.</p>
<p>If the iterable is made of strings the third parameter can be used to
control the case sensitiveness of the comparison which is disabled by
default.</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">iterable</span><span class="o">|</span><span class="nf">sort</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> ...</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>It is also possible to sort by an attribute (for example to sort
by the date of an object) by specifying the <cite>attribute</cite> parameter:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">iterable</span><span class="o">|</span><span class="nf">sort</span><span class="o">(</span><span class="nv">attribute</span><span class="o">=</span><span class="s1">'date'</span><span class="o">)</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> ...</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.6: </span>The <cite>attribute</cite> parameter was added.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="string">
<code class="descname">string</code><span class="sig-paren">(</span><em>object</em><span class="sig-paren">)</span><a class="headerlink" href="#string" title="Permalink to this definition">¶</a></dt>
<dd><p>Make a string unicode if it isn’t already. That way a markup
string is not converted back to unicode.</p>
</dd></dl>
<dl class="function">
<dt id="striptags">
<code class="descname">striptags</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#striptags" title="Permalink to this definition">¶</a></dt>
<dd><p>Strip SGML/XML tags and replace adjacent whitespace by one space.</p>
</dd></dl>
<dl class="function">
<dt id="sum">
<code class="descname">sum</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#sum" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the sum of a sequence of numbers plus the value of parameter
‘start’ (which defaults to 0). When the sequence is empty it returns
start.</p>
<p>It is also possible to sum up only certain attributes:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="x">Total: </span><span class="cp">{{</span> <span class="nv">items</span><span class="o">|</span><span class="nf">sum</span><span class="o">(</span><span class="nv">attribute</span><span class="o">=</span><span class="s1">'price'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.6: </span>The <cite>attribute</cite> parameter was added to allow suming up over
attributes. Also the <cite>start</cite> parameter was moved on to the right.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="title">
<code class="descname">title</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#title" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a titlecased version of the value. I.e. words will start with
uppercase letters, all remaining characters are lowercase.</p>
</dd></dl>
<dl class="function">
<dt id="tojson">
<code class="descname">tojson</code><span class="sig-paren">(</span><em>value</em>, <em>indent=None</em><span class="sig-paren">)</span><a class="headerlink" href="#tojson" title="Permalink to this definition">¶</a></dt>
<dd><p>Dumps a structure to JSON so that it’s safe to use in <code class="docutils literal notranslate"><span class="pre"><script></span></code>
tags. It accepts the same arguments and returns a JSON string. Note that
this is available in templates through the <code class="docutils literal notranslate"><span class="pre">|tojson</span></code> filter which will
also mark the result as safe. Due to how this function escapes certain
characters this is safe even if used outside of <code class="docutils literal notranslate"><span class="pre"><script></span></code> tags.</p>
<p>The following characters are escaped in strings:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre"><</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">></span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">&</span></code></li>
<li><code class="docutils literal notranslate"><span class="pre">'</span></code></li>
</ul>
<p>This makes it safe to embed such strings in any place in HTML with the
notable exception of double quoted attributes. In that case single
quote your attributes or HTML escape it in addition.</p>
<p>The indent parameter can be used to enable pretty printing. Set it to
the number of spaces that the structures should be indented with.</p>
<p>Note that this filter is for use in HTML contexts only.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.9.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="trim">
<code class="descname">trim</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#trim" title="Permalink to this definition">¶</a></dt>
<dd><p>Strip leading and trailing whitespace.</p>
</dd></dl>
<dl class="function">
<dt id="truncate">
<code class="descname">truncate</code><span class="sig-paren">(</span><em>s</em>, <em>length=255</em>, <em>killwords=False</em>, <em>end='...'</em>, <em>leeway=None</em><span class="sig-paren">)</span><a class="headerlink" href="#truncate" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a truncated copy of the string. The length is specified
with the first parameter which defaults to <code class="docutils literal notranslate"><span class="pre">255</span></code>. If the second
parameter is <code class="docutils literal notranslate"><span class="pre">true</span></code> the filter will cut the text at length. Otherwise
it will discard the last word. If the text was in fact
truncated it will append an ellipsis sign (<code class="docutils literal notranslate"><span class="pre">"..."</span></code>). If you want a
different ellipsis sign than <code class="docutils literal notranslate"><span class="pre">"..."</span></code> you can specify it using the
third parameter. Strings that only exceed the length by the tolerance
margin given in the fourth parameter will not be truncated.</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="s2">"foo bar baz qux"</span><span class="o">|</span><span class="nf">truncate</span><span class="o">(</span><span class="m">9</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> "foo..."</span>
<span class="cp">{{</span> <span class="s2">"foo bar baz qux"</span><span class="o">|</span><span class="nf">truncate</span><span class="o">(</span><span class="m">9</span><span class="o">,</span> <span class="kp">True</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> "foo ba..."</span>
<span class="cp">{{</span> <span class="s2">"foo bar baz qux"</span><span class="o">|</span><span class="nf">truncate</span><span class="o">(</span><span class="m">11</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> "foo bar baz qux"</span>
<span class="cp">{{</span> <span class="s2">"foo bar baz qux"</span><span class="o">|</span><span class="nf">truncate</span><span class="o">(</span><span class="m">11</span><span class="o">,</span> <span class="kp">False</span><span class="o">,</span> <span class="s1">'...'</span><span class="o">,</span> <span class="m">0</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> "foo bar..."</span>
</pre></div>
</div>
<p>The default leeway on newer Jinja2 versions is 5 and was 0 before but
can be reconfigured globally.</p>
</dd></dl>
<dl class="function">
<dt id="unique">
<code class="descname">unique</code><span class="sig-paren">(</span><em>value</em>, <em>case_sensitive=False</em>, <em>attribute=None</em><span class="sig-paren">)</span><a class="headerlink" href="#unique" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a list of unique items from the the given iterable.</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="o">[</span><span class="s1">'foo'</span><span class="o">,</span> <span class="s1">'bar'</span><span class="o">,</span> <span class="s1">'foobar'</span><span class="o">,</span> <span class="s1">'FooBar'</span><span class="o">]|</span><span class="nf">unique</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> -> ['foo', 'bar', 'foobar']</span>
</pre></div>
</div>
<p>The unique items are yielded in the same order as their first occurrence in
the iterable passed to the filter.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>case_sensitive</strong> – Treat upper and lower case strings as distinct.</li>
<li><strong>attribute</strong> – Filter objects with unique values for this attribute.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="upper">
<code class="descname">upper</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#upper" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a value to uppercase.</p>
</dd></dl>
<dl class="function">
<dt id="urlencode">
<code class="descname">urlencode</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#urlencode" title="Permalink to this definition">¶</a></dt>
<dd><p>Escape strings for use in URLs (uses UTF-8 encoding). It accepts both
dictionaries and regular strings as well as pairwise iterables.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="urlize">
<code class="descname">urlize</code><span class="sig-paren">(</span><em>value</em>, <em>trim_url_limit=None</em>, <em>nofollow=False</em>, <em>target=None</em>, <em>rel=None</em><span class="sig-paren">)</span><a class="headerlink" href="#urlize" title="Permalink to this definition">¶</a></dt>
<dd><p>Converts URLs in plain text into clickable links.</p>
<p>If you pass the filter an additional integer it will shorten the urls
to that number. Also a third argument exists that makes the urls
“nofollow”:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">mytext</span><span class="o">|</span><span class="nf">urlize</span><span class="o">(</span><span class="m">40</span><span class="o">,</span> <span class="kp">true</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
<span class="x"> links are shortened to 40 chars and defined with rel="nofollow"</span>
</pre></div>
</div>
<p>If <em>target</em> is specified, the <code class="docutils literal notranslate"><span class="pre">target</span></code> attribute will be added to the
<code class="docutils literal notranslate"><span class="pre"><a></span></code> tag:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">mytext</span><span class="o">|</span><span class="nf">urlize</span><span class="o">(</span><span class="m">40</span><span class="o">,</span> <span class="nv">target</span><span class="o">=</span><span class="s1">'_blank'</span><span class="o">)</span> <span class="cp">}}</span><span class="x"></span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.8+: </span>The <em>target</em> parameter was added.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="wordcount">
<code class="descname">wordcount</code><span class="sig-paren">(</span><em>s</em><span class="sig-paren">)</span><a class="headerlink" href="#wordcount" title="Permalink to this definition">¶</a></dt>
<dd><p>Count the words in that string.</p>
</dd></dl>
<dl class="function">
<dt id="wordwrap">
<code class="descname">wordwrap</code><span class="sig-paren">(</span><em>s</em>, <em>width=79</em>, <em>break_long_words=True</em>, <em>wrapstring=None</em><span class="sig-paren">)</span><a class="headerlink" href="#wordwrap" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a copy of the string passed to the filter wrapped after
<code class="docutils literal notranslate"><span class="pre">79</span></code> characters. You can override this default using the first
parameter. If you set the second parameter to <cite>false</cite> Jinja will not
split words apart if they are longer than <cite>width</cite>. By default, the newlines
will be the default newlines for the environment, but this can be changed
using the wrapstring keyword argument.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.7: </span>Added support for the <cite>wrapstring</cite> parameter.</p>
</div>
</dd></dl>
<dl class="function">
<dt id="xmlattr">
<code class="descname">xmlattr</code><span class="sig-paren">(</span><em>d</em>, <em>autospace=True</em><span class="sig-paren">)</span><a class="headerlink" href="#xmlattr" title="Permalink to this definition">¶</a></dt>
<dd><p>Create an SGML/XML attribute string based on the items in a dict.</p>
<p><strong>Values</strong> that are neither <code class="docutils literal notranslate"><span class="pre">none</span></code> nor <code class="docutils literal notranslate"><span class="pre">undefined</span></code> are automatically
escaped, safely allowing untrusted user input.</p>
<p>User input should not be used as <strong>keys</strong> to this filter. If any key
contains a space, <code class="docutils literal notranslate"><span class="pre">/</span></code> solidus, <code class="docutils literal notranslate"><span class="pre">></span></code> greater-than sign, or <code class="docutils literal notranslate"><span class="pre">=</span></code> equals
sign, this fails with a <code class="docutils literal notranslate"><span class="pre">ValueError</span></code>. Regardless of this, user input
should never be used as keys to this filter, or must be separately validated
first.</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="cp">{{</span> <span class="o">{</span><span class="s1">'class'</span><span class="o">:</span> <span class="s1">'my_list'</span><span class="o">,</span> <span class="s1">'missing'</span><span class="o">:</span> <span class="kp">none</span><span class="o">,</span>
<span class="s1">'id'</span><span class="o">:</span> <span class="s1">'list-%d'</span><span class="o">|</span><span class="nf">format</span><span class="o">(</span><span class="nv">variable</span><span class="o">)}|</span><span class="nf">xmlattr</span> <span class="cp">}}</span><span class="p">></span>
...
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>Results in something like this:</p>
<div class="highlight-html notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span> <span class="na">class</span><span class="o">=</span><span class="s">"my_list"</span> <span class="na">id</span><span class="o">=</span><span class="s">"list-42"</span><span class="p">></span>
...
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>As you can see it automatically prepends a space in front of the item
if the filter returned something unless the second parameter is false.</p>
</dd></dl>
</div>
<div class="section" id="list-of-builtin-tests">
<span id="builtin-tests"></span><h2>List of Builtin Tests<a class="headerlink" href="#list-of-builtin-tests" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="callable">
<code class="descname">callable</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#callable" title="Permalink to this definition">¶</a></dt>
<dd><p>Return whether the object is callable (i.e., some kind of function).</p>
<p>Note that classes are callable, as are instances of classes with a
__call__() method.</p>
</dd></dl>
<dl class="function">
<dt id="defined">
<code class="descname">defined</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#defined" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the variable is defined:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">variable</span> <span class="k">is</span> <span class="nf">defined</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> value of variable: </span><span class="cp">{{</span> <span class="nv">variable</span> <span class="cp">}}</span><span class="x"></span>
<span class="cp">{%</span> <span class="k">else</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> variable is not defined</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
<p>See the <a class="reference internal" href="#default" title="default"><code class="xref py py-func docutils literal notranslate"><span class="pre">default()</span></code></a> filter for a simple way to set undefined
variables.</p>
</dd></dl>
<dl class="function">
<dt id="divisibleby">
<code class="descname">divisibleby</code><span class="sig-paren">(</span><em>value</em>, <em>num</em><span class="sig-paren">)</span><a class="headerlink" href="#divisibleby" title="Permalink to this definition">¶</a></dt>
<dd><p>Check if a variable is divisible by a number.</p>
</dd></dl>
<dl class="function">
<dt id="eq">
<code class="descname">eq</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#eq" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre">==</span></code>, <code class="docutils literal notranslate"><span class="pre">equalto</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="escaped">
<code class="descname">escaped</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#escaped" title="Permalink to this definition">¶</a></dt>
<dd><p>Check if the value is escaped.</p>
</dd></dl>
<dl class="function">
<dt id="even">
<code class="descname">even</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#even" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the variable is even.</p>
</dd></dl>
<dl class="function">
<dt id="ge">
<code class="descname">ge</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#ge" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre">>=</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="gt">
<code class="descname">gt</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#gt" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre">></span></code>, <code class="docutils literal notranslate"><span class="pre">greaterthan</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="in">
<code class="descname">in</code><span class="sig-paren">(</span><em>value</em>, <em>seq</em><span class="sig-paren">)</span><a class="headerlink" href="#in" title="Permalink to this definition">¶</a></dt>
<dd><p>Check if value is in seq.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.10.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="iterable">
<code class="descname">iterable</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#iterable" title="Permalink to this definition">¶</a></dt>
<dd><p>Check if it’s possible to iterate over an object.</p>
</dd></dl>
<dl class="function">
<dt id="le">
<code class="descname">le</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#le" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre"><=</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">lower</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span></dt>
<dd><p>Return true if the variable is lowercased.</p>
</dd></dl>
<dl class="function">
<dt id="lt">
<code class="descname">lt</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#lt" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre"><</span></code>, <code class="docutils literal notranslate"><span class="pre">lessthan</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="mapping">
<code class="descname">mapping</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#mapping" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the object is a mapping (dict etc.).</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.6.</span></p>
</div>
</dd></dl>
<dl class="function">
<dt id="ne">
<code class="descname">ne</code><span class="sig-paren">(</span><em>a</em>, <em>b</em><span class="sig-paren">)</span><a class="headerlink" href="#ne" title="Permalink to this definition">¶</a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Aliases:</th><td class="field-body"><code class="docutils literal notranslate"><span class="pre">!=</span></code></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="none">
<code class="descname">none</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#none" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the variable is none.</p>
</dd></dl>
<dl class="function">
<dt id="number">
<code class="descname">number</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#number" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the variable is a number.</p>
</dd></dl>
<dl class="function">
<dt id="odd">
<code class="descname">odd</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#odd" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the variable is odd.</p>
</dd></dl>
<dl class="function">
<dt id="sameas">
<code class="descname">sameas</code><span class="sig-paren">(</span><em>value</em>, <em>other</em><span class="sig-paren">)</span><a class="headerlink" href="#sameas" title="Permalink to this definition">¶</a></dt>
<dd><p>Check if an object points to the same memory address than another
object:</p>
<div class="highlight-jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">if</span> <span class="nv">foo.attribute</span> <span class="k">is</span> <span class="nf">sameas</span> <span class="kp">false</span> <span class="cp">%}</span><span class="x"></span>
<span class="x"> the foo attribute really is the `False` singleton</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span><span class="x"></span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="sequence">
<code class="descname">sequence</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#sequence" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the variable is a sequence. Sequences are variables
that are iterable.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">string</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span></dt>
<dd><p>Return true if the object is a string.</p>
</dd></dl>
<dl class="function">
<dt id="undefined">
<code class="descname">undefined</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#undefined" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#defined" title="defined"><code class="xref py py-func docutils literal notranslate"><span class="pre">defined()</span></code></a> but the other way round.</p>
</dd></dl>
<dl class="function">
<dt>
<code class="descname">upper</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span></dt>
<dd><p>Return true if the variable is uppercased.</p>
</dd></dl>
</div>
<div class="section" id="list-of-global-functions">
<span id="builtin-globals"></span><h2>List of Global Functions<a class="headerlink" href="#list-of-global-functions" title="Permalink to this headline">¶</a></h2>
<p>The following functions are available in the global scope by default:</p>
<dl class="function">
<dt id="range">
<code class="descname">range</code><span class="sig-paren">(</span><span class="optional">[</span><em>start</em>, <span class="optional">]</span><em>stop</em><span class="optional">[</span>, <em>step</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#range" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a list containing an arithmetic progression of integers.
<code class="docutils literal notranslate"><span class="pre">range(i,</span> <span class="pre">j)</span></code> returns <code class="docutils literal notranslate"><span class="pre">[i,</span> <span class="pre">i+1,</span> <span class="pre">i+2,</span> <span class="pre">...,</span> <span class="pre">j-1]</span></code>;
start (!) defaults to <code class="docutils literal notranslate"><span class="pre">0</span></code>.
When step is given, it specifies the increment (or decrement).
For example, <code class="docutils literal notranslate"><span class="pre">range(4)</span></code> and <code class="docutils literal notranslate"><span class="pre">range(0,</span> <span class="pre">4,</span> <span class="pre">1)</span></code> return <code class="docutils literal notranslate"><span class="pre">[0,</span> <span class="pre">1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code>.
The end point is omitted!
These are exactly the valid indices for a list of 4 elements.</p>
<p>This is useful to repeat a template block multiple times, e.g.
to fill a list. Imagine you have 7 users in the list but you want to
render three empty items to enforce a height with CSS:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">ul</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span><span class="p">></span><span class="cp">{{</span> <span class="nv">user.username</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">number</span> <span class="k">in</span> <span class="nv">range</span><span class="o">(</span><span class="m">10</span> <span class="o">-</span> <span class="nv">users</span><span class="o">|</span><span class="nf">count</span><span class="o">)</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span> <span class="na">class</span><span class="o">=</span><span class="s">"empty"</span><span class="p">><</span><span class="nt">span</span><span class="p">></span>...<span class="p"></</span><span class="nt">span</span><span class="p">></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
<dt id="lipsum">
<code class="descname">lipsum</code><span class="sig-paren">(</span><em>n=5</em>, <em>html=True</em>, <em>min=20</em>, <em>max=100</em><span class="sig-paren">)</span><a class="headerlink" href="#lipsum" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates some lorem ipsum for the template. By default, five paragraphs
of HTML are generated with each paragraph between 20 and 100 words.
If html is False, regular text is returned. This is useful to generate simple
contents for layout testing.</p>
</dd></dl>
<dl class="function">
<dt id="dict">
<code class="descname">dict</code><span class="sig-paren">(</span><em>**items</em><span class="sig-paren">)</span><a class="headerlink" href="#dict" title="Permalink to this definition">¶</a></dt>
<dd><p>A convenient alternative to dict literals. <code class="docutils literal notranslate"><span class="pre">{'foo':</span> <span class="pre">'bar'}</span></code> is the same
as <code class="docutils literal notranslate"><span class="pre">dict(foo='bar')</span></code>.</p>
</dd></dl>
<dl class="class">
<dt id="cycler">
<em class="property">class </em><code class="descname">cycler</code><span class="sig-paren">(</span><em>*items</em><span class="sig-paren">)</span><a class="headerlink" href="#cycler" title="Permalink to this definition">¶</a></dt>
<dd><p>The cycler allows you to cycle among values similar to how <cite>loop.cycle</cite>
works. Unlike <cite>loop.cycle</cite>, you can use this cycler outside of
loops or over multiple loops.</p>
<p>This can be very useful if you want to show a list of folders and
files with the folders on top but both in the same list with alternating
row colors.</p>
<p>The following example shows how <cite>cycler</cite> can be used:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">row_class</span> <span class="o">=</span> <span class="nv">cycler</span><span class="o">(</span><span class="s1">'odd'</span><span class="o">,</span> <span class="s1">'even'</span><span class="o">)</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">ul</span> <span class="na">class</span><span class="o">=</span><span class="s">"browser"</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">folder</span> <span class="k">in</span> <span class="nv">folders</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span> <span class="na">class</span><span class="o">=</span><span class="s">"folder </span><span class="cp">{{</span> <span class="nv">row_class.next</span><span class="o">()</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">folder</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">filename</span> <span class="k">in</span> <span class="nv">files</span> <span class="cp">%}</span>
<span class="p"><</span><span class="nt">li</span> <span class="na">class</span><span class="o">=</span><span class="s">"file </span><span class="cp">{{</span> <span class="nv">row_class.next</span><span class="o">()</span> <span class="cp">}}</span><span class="s">"</span><span class="p">></span><span class="cp">{{</span> <span class="nv">filename</span><span class="o">|</span><span class="nf">e</span> <span class="cp">}}</span><span class="p"></</span><span class="nt">li</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
<span class="p"></</span><span class="nt">ul</span><span class="p">></span>
</pre></div>
</div>
<p>A cycler has the following attributes and methods:</p>
<dl class="method">
<dt id="cycler.reset">
<code class="descname">reset</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cycler.reset" title="Permalink to this definition">¶</a></dt>
<dd><p>Resets the cycle to the first item.</p>
</dd></dl>
<dl class="method">
<dt id="cycler.next">
<code class="descname">next</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#cycler.next" title="Permalink to this definition">¶</a></dt>
<dd><p>Goes one item ahead and returns the then-current item.</p>
</dd></dl>
<dl class="attribute">
<dt id="cycler.current">
<code class="descname">current</code><a class="headerlink" href="#cycler.current" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the current item.</p>
</dd></dl>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd></dl>
<dl class="class">
<dt id="joiner">
<em class="property">class </em><code class="descname">joiner</code><span class="sig-paren">(</span><em>sep='</em>, <em>'</em><span class="sig-paren">)</span><a class="headerlink" href="#joiner" title="Permalink to this definition">¶</a></dt>
<dd><p>A tiny helper that can be used to “join” multiple sections. A joiner is
passed a string and will return that string every time it’s called, except
the first time (in which case it returns an empty string). You can
use this to join things:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">pipe</span> <span class="o">=</span> <span class="nv">joiner</span><span class="o">(</span><span class="s2">"|"</span><span class="o">)</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">categories</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">pipe</span><span class="o">()</span> <span class="cp">}}</span>
Categories: <span class="cp">{{</span> <span class="nv">categories</span><span class="o">|</span><span class="nf">join</span><span class="o">(</span><span class="s2">", "</span><span class="o">)</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">author</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">pipe</span><span class="o">()</span> <span class="cp">}}</span>
Author: <span class="cp">{{</span> <span class="nv">author</span><span class="o">()</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">can_edit</span> <span class="cp">%}</span> <span class="cp">{{</span> <span class="nv">pipe</span><span class="o">()</span> <span class="cp">}}</span>
<span class="p"><</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">"?action=edit"</span><span class="p">></span>Edit<span class="p"></</span><span class="nt">a</span><span class="p">></span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.1.</span></p>
</div>
</dd></dl>
<dl class="class">
<dt id="namespace">
<em class="property">class </em><code class="descname">namespace</code><span class="sig-paren">(</span><em>...</em><span class="sig-paren">)</span><a class="headerlink" href="#namespace" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a new container that allows attribute assignment using the
<code class="docutils literal notranslate"><span class="pre">{%</span> <span class="pre">set</span> <span class="pre">%}</span></code> tag:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">ns</span> <span class="o">=</span> <span class="nv">namespace</span><span class="o">()</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">ns.foo</span> <span class="o">=</span> <span class="s1">'bar'</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>The main purpose of this is to allow carrying a value from within a loop
body to an outer scope. Initial values can be provided as a dict, as
keyword arguments, or both (same behavior as Python’s <cite>dict</cite> constructor):</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">set</span> <span class="nv">ns</span> <span class="o">=</span> <span class="nv">namespace</span><span class="o">(</span><span class="nv">found</span><span class="o">=</span><span class="kp">false</span><span class="o">)</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">for</span> <span class="nv">item</span> <span class="k">in</span> <span class="nv">items</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">if</span> <span class="nv">item.check_something</span><span class="o">()</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">ns.found</span> <span class="o">=</span> <span class="kp">true</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endif</span> <span class="cp">%}</span>
* <span class="cp">{{</span> <span class="nv">item.title</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
Found item having something: <span class="cp">{{</span> <span class="nv">ns.found</span> <span class="cp">}}</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.10.</span></p>
</div>
</dd></dl>
</div>
<div class="section" id="extensions">
<h2>Extensions<a class="headerlink" href="#extensions" title="Permalink to this headline">¶</a></h2>
<p>The following sections cover the built-in Jinja2 extensions that may be
enabled by an application. An application could also provide further
extensions not covered by this documentation; in which case there should
be a separate document explaining said <a class="reference internal" href="extensions.html#jinja-extensions"><span class="std std-ref">extensions</span></a>.</p>
<div class="section" id="i18n">
<span id="i18n-in-templates"></span><h3>i18n<a class="headerlink" href="#i18n" title="Permalink to this headline">¶</a></h3>
<p>If the i18n extension is enabled, it’s possible to mark parts in the template
as translatable. To mark a section as translatable, you can use <cite>trans</cite>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{%</span> <span class="k">trans</span> <span class="cp">%}</span>Hello <span class="cp">{{</span> <span class="nv">user</span> <span class="cp">}}</span>!<span class="cp">{%</span> <span class="k">endtrans</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span>
</pre></div>
</div>
<p>To translate a template expression — say, using template filters, or by just
accessing an attribute of an object — you need to bind the expression to a
name for use within the translation block:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="p"><</span><span class="nt">p</span><span class="p">></span><span class="cp">{%</span> <span class="k">trans</span> <span class="nv">user</span><span class="o">=</span><span class="nv">user.username</span> <span class="cp">%}</span>Hello <span class="cp">{{</span> <span class="nv">user</span> <span class="cp">}}</span>!<span class="cp">{%</span> <span class="k">endtrans</span> <span class="cp">%}</span><span class="p"></</span><span class="nt">p</span><span class="p">></span>
</pre></div>
</div>
<p>If you need to bind more than one expression inside a <cite>trans</cite> tag, separate
the pieces with a comma (<code class="docutils literal notranslate"><span class="pre">,</span></code>):</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">trans</span> <span class="nv">book_title</span><span class="o">=</span><span class="nv">book.title</span><span class="o">,</span> <span class="nv">author</span><span class="o">=</span><span class="nv">author.name</span> <span class="cp">%}</span>
This is <span class="cp">{{</span> <span class="nv">book_title</span> <span class="cp">}}</span> by <span class="cp">{{</span> <span class="nv">author</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endtrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Inside trans tags no statements are allowed, only variable tags are.</p>
<p>To pluralize, specify both the singular and plural forms with the <cite>pluralize</cite>
tag, which appears between <cite>trans</cite> and <cite>endtrans</cite>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">trans</span> <span class="nv">count</span><span class="o">=</span><span class="nv">list</span><span class="o">|</span><span class="nf">length</span> <span class="cp">%}</span>
There is <span class="cp">{{</span> <span class="nv">count</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span> object.
<span class="cp">{%</span> <span class="k">pluralize</span> <span class="cp">%}</span>
There are <span class="cp">{{</span> <span class="nv">count</span> <span class="cp">}}</span> <span class="cp">{{</span> <span class="nv">name</span> <span class="cp">}}</span> objects.
<span class="cp">{%</span> <span class="k">endtrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>By default, the first variable in a block is used to determine the correct
singular or plural form. If that doesn’t work out, you can specify the name
which should be used for pluralizing by adding it as parameter to <cite>pluralize</cite>:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">trans</span> <span class="p">...</span><span class="o">,</span> <span class="nv">user_count</span><span class="o">=</span><span class="nv">users</span><span class="o">|</span><span class="nf">length</span> <span class="cp">%}</span>...
<span class="cp">{%</span> <span class="k">pluralize</span> <span class="nv">user_count</span> <span class="cp">%}</span>...<span class="cp">{%</span> <span class="k">endtrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>When translating longer blocks of text, whitespace and linebreaks result in
rather ugly and error-prone translation strings. To avoid this, a trans block
can be marked as trimmed which will replace all linebreaks and the whitespace
surrounding them with a single space and remove leading/trailing whitespace:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">trans</span> <span class="nv">trimmed</span> <span class="nv">book_title</span><span class="o">=</span><span class="nv">book.title</span> <span class="cp">%}</span>
This is <span class="cp">{{</span> <span class="nv">book_title</span> <span class="cp">}}</span>.
You should read it!
<span class="cp">{%</span> <span class="k">endtrans</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>If trimming is enabled globally, the <cite>notrimmed</cite> modifier can be used to
disable it for a <cite>trans</cite> block.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.10: </span>The <cite>trimmed</cite> and <cite>notrimmed</cite> modifiers have been added.</p>
</div>
<p>It’s also possible to translate strings in expressions. For that purpose,
three functions exist:</p>
<ul class="simple">
<li><cite>gettext</cite>: translate a single string</li>
<li><cite>ngettext</cite>: translate a pluralizable string</li>
<li><cite>_</cite>: alias for <cite>gettext</cite></li>
</ul>
<p>For example, you can easily print a translated string like this:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="kp">_</span><span class="o">(</span><span class="s1">'Hello World!'</span><span class="o">)</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>To use placeholders, use the <cite>format</cite> filter:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="kp">_</span><span class="o">(</span><span class="s1">'Hello %(user)s!'</span><span class="o">)|</span><span class="nf">format</span><span class="o">(</span><span class="nv">user</span><span class="o">=</span><span class="nv">user.username</span><span class="o">)</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>For multiple placeholders, always use keyword arguments to <cite>format</cite>,
as other languages may not use the words in the same order.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.5.</span></p>
</div>
<p>If newstyle gettext calls are activated (<a class="reference internal" href="extensions.html#newstyle-gettext"><span class="std std-ref">Whitespace Trimming</span></a>), using
placeholders is a lot easier:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{{</span> <span class="nv">gettext</span><span class="o">(</span><span class="s1">'Hello World!'</span><span class="o">)</span> <span class="cp">}}</span>
<span class="cp">{{</span> <span class="nv">gettext</span><span class="o">(</span><span class="s1">'Hello %(name)s!'</span><span class="o">,</span> <span class="nv">name</span><span class="o">=</span><span class="s1">'World'</span><span class="o">)</span> <span class="cp">}}</span>
<span class="cp">{{</span> <span class="nv">ngettext</span><span class="o">(</span><span class="s1">'%(num)d apple'</span><span class="o">,</span> <span class="s1">'%(num)d apples'</span><span class="o">,</span> <span class="nv">apples</span><span class="o">|</span><span class="nf">count</span><span class="o">)</span> <span class="cp">}}</span>
</pre></div>
</div>
<p>Note that the <cite>ngettext</cite> function’s format string automatically receives
the count as a <cite>num</cite> parameter in addition to the regular parameters.</p>
</div>
<div class="section" id="expression-statement">
<h3>Expression Statement<a class="headerlink" href="#expression-statement" title="Permalink to this headline">¶</a></h3>
<p>If the expression-statement extension is loaded, a tag called <cite>do</cite> is available
that works exactly like the regular variable expression (<code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">...</span> <span class="pre">}}</span></code>); except
it doesn’t print anything. This can be used to modify lists:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">do</span> <span class="nv">navigation.append</span><span class="o">(</span><span class="s1">'a string'</span><span class="o">)</span> <span class="cp">%}</span>
</pre></div>
</div>
</div>
<div class="section" id="loop-controls">
<h3>Loop Controls<a class="headerlink" href="#loop-controls" title="Permalink to this headline">¶</a></h3>
<p>If the application enables the <a class="reference internal" href="extensions.html#loopcontrols-extension"><span class="std std-ref">Loop Controls</span></a>, it’s possible to
use <cite>break</cite> and <cite>continue</cite> in loops. When <cite>break</cite> is reached, the loop is
terminated; if <cite>continue</cite> is reached, the processing is stopped and continues
with the next iteration.</p>
<p>Here’s a loop that skips every second item:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="cp">{%</span>- <span class="k">if</span> <span class="nb">loop</span><span class="nv">.index</span> <span class="k">is</span> <span class="nf">even</span> <span class="cp">%}{%</span> <span class="k">continue</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}</span>
...
<span class="cp">{%</span> <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Likewise, a loop that stops processing after the 10th iteration:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">for</span> <span class="nv">user</span> <span class="k">in</span> <span class="nv">users</span> <span class="cp">%}</span>
<span class="cp">{%</span>- <span class="k">if</span> <span class="nb">loop</span><span class="nv">.index</span> <span class="o">>=</span> <span class="m">10</span> <span class="cp">%}{%</span> <span class="k">break</span> <span class="cp">%}{%</span> <span class="k">endif</span> <span class="cp">%}</span>
<span class="cp">{%</span>- <span class="k">endfor</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>Note that <code class="docutils literal notranslate"><span class="pre">loop.index</span></code> starts with 1, and <code class="docutils literal notranslate"><span class="pre">loop.index0</span></code> starts with 0
(See: <a class="reference internal" href="#for-loop"><span class="std std-ref">For</span></a>).</p>
</div>
<div class="section" id="with-statement">
<h3>With Statement<a class="headerlink" href="#with-statement" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.3.</span></p>
</div>
<p>The with statement makes it possible to create a new inner scope.
Variables set within this scope are not visible outside of the scope.</p>
<p>With in a nutshell:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">foo</span> <span class="o">=</span> <span class="m">42</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">foo</span> <span class="cp">}}</span> foo is 42 here
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
foo is not visible here any longer
</pre></div>
</div>
<p>Because it is common to set variables at the beginning of the scope,
you can do that within the <cite>with</cite> statement. The following two examples
are equivalent:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="nv">foo</span> <span class="o">=</span> <span class="m">42</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">foo</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">with</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">foo</span> <span class="o">=</span> <span class="m">42</span> <span class="cp">%}</span>
<span class="cp">{{</span> <span class="nv">foo</span> <span class="cp">}}</span>
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>An important note on scoping here. In Jinja versions before 2.9 the
behavior of referencing one variable to another had some unintended
consequences. In particular one variable could refer to another defined
in the same with block’s opening statement. This caused issues with the
cleaned up scoping behavior and has since been improved. In particular
in newer Jinja2 versions the following code always refers to the variable
<cite>a</cite> from outside the <cite>with</cite> block:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="nv">a</span><span class="o">={},</span> <span class="nv">b</span><span class="o">=</span><span class="nv">a.attribute</span> <span class="cp">%}</span>...<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>In earlier Jinja versions the <cite>b</cite> attribute would refer to the results of
the first attribute. If you depend on this behavior you can rewrite it to
use the <code class="docutils literal notranslate"><span class="pre">set</span></code> tag:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">with</span> <span class="nv">a</span><span class="o">={}</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">set</span> <span class="nv">b</span> <span class="o">=</span> <span class="nv">a.attribute</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">endwith</span> <span class="cp">%}</span>
</pre></div>
</div>
<div class="admonition-extension admonition">
<p class="first admonition-title">Extension</p>
<p class="last">In older versions of Jinja (before 2.9) it was required to enable this
feature with an extension. It’s now enabled by default.</p>
</div>
</div>
</div>
<div class="section" id="autoescape-overrides">
<span id="id18"></span><h2>Autoescape Overrides<a class="headerlink" href="#autoescape-overrides" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.4.</span></p>
</div>
<p>If you want you can activate and deactivate the autoescaping from within
the templates.</p>
<p>Example:</p>
<div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">autoescape</span> <span class="kp">true</span> <span class="cp">%}</span>
Autoescaping is active within this block
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
<span class="cp">{%</span> <span class="k">autoescape</span> <span class="kp">false</span> <span class="cp">%}</span>
Autoescaping is inactive within this block
<span class="cp">{%</span> <span class="k">endautoescape</span> <span class="cp">%}</span>
</pre></div>
</div>
<p>After an <cite>endautoescape</cite> the behavior is reverted to what it was before.</p>
<div class="admonition-extension admonition">
<p class="first admonition-title">Extension</p>
<p class="last">In older versions of Jinja (before 2.9) it was required to enable this
feature with an extension. It’s now enabled by default.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper"><p class="logo"><a href="index.html">
<img class="logo" src="_static/jinja-small.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Template Designer Documentation</a><ul>
<li><a class="reference internal" href="#synopsis">Synopsis</a></li>
<li><a class="reference internal" href="#variables">Variables</a></li>
<li><a class="reference internal" href="#filters">Filters</a></li>
<li><a class="reference internal" href="#tests">Tests</a></li>
<li><a class="reference internal" href="#comments">Comments</a></li>
<li><a class="reference internal" href="#whitespace-control">Whitespace Control</a></li>
<li><a class="reference internal" href="#escaping">Escaping</a></li>
<li><a class="reference internal" href="#line-statements">Line Statements</a></li>
<li><a class="reference internal" href="#template-inheritance">Template Inheritance</a><ul>
<li><a class="reference internal" href="#base-template">Base Template</a></li>
<li><a class="reference internal" href="#child-template">Child Template</a></li>
<li><a class="reference internal" href="#super-blocks">Super Blocks</a></li>
<li><a class="reference internal" href="#named-block-end-tags">Named Block End-Tags</a></li>
<li><a class="reference internal" href="#block-nesting-and-scope">Block Nesting and Scope</a></li>
<li><a class="reference internal" href="#template-objects">Template Objects</a></li>
</ul>
</li>
<li><a class="reference internal" href="#html-escaping">HTML Escaping</a><ul>
<li><a class="reference internal" href="#working-with-manual-escaping">Working with Manual Escaping</a></li>
<li><a class="reference internal" href="#working-with-automatic-escaping">Working with Automatic Escaping</a></li>
</ul>
</li>
<li><a class="reference internal" href="#list-of-control-structures">List of Control Structures</a><ul>
<li><a class="reference internal" href="#for">For</a></li>
<li><a class="reference internal" href="#if">If</a></li>
<li><a class="reference internal" href="#macros">Macros</a></li>
<li><a class="reference internal" href="#call">Call</a></li>
<li><a class="reference internal" href="#id11">Filters</a></li>
<li><a class="reference internal" href="#assignments">Assignments</a></li>
<li><a class="reference internal" href="#block-assignments">Block Assignments</a></li>
<li><a class="reference internal" href="#extends">Extends</a></li>
<li><a class="reference internal" href="#blocks">Blocks</a></li>
<li><a class="reference internal" href="#include">Include</a></li>
<li><a class="reference internal" href="#import">Import</a></li>
</ul>
</li>
<li><a class="reference internal" href="#import-context-behavior">Import Context Behavior</a></li>
<li><a class="reference internal" href="#expressions">Expressions</a><ul>
<li><a class="reference internal" href="#literals">Literals</a></li>
<li><a class="reference internal" href="#math">Math</a></li>
<li><a class="reference internal" href="#comparisons">Comparisons</a></li>
<li><a class="reference internal" href="#logic">Logic</a></li>
<li><a class="reference internal" href="#other-operators">Other Operators</a></li>
<li><a class="reference internal" href="#if-expression">If Expression</a></li>
</ul>
</li>
<li><a class="reference internal" href="#list-of-builtin-filters">List of Builtin Filters</a></li>
<li><a class="reference internal" href="#list-of-builtin-tests">List of Builtin Tests</a></li>
<li><a class="reference internal" href="#list-of-global-functions">List of Global Functions</a></li>
<li><a class="reference internal" href="#extensions">Extensions</a><ul>
<li><a class="reference internal" href="#i18n">i18n</a></li>
<li><a class="reference internal" href="#expression-statement">Expression Statement</a></li>
<li><a class="reference internal" href="#loop-controls">Loop Controls</a></li>
<li><a class="reference internal" href="#with-statement">With Statement</a></li>
</ul>
</li>
<li><a class="reference internal" href="#autoescape-overrides">Autoescape Overrides</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="nativetypes.html" title="previous chapter">Native Python Types</a></li>
<li>Next: <a href="extensions.html" title="next chapter">Extensions</a></li>
</ul></li>
</ul>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/templates.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2008, Armin Ronacher.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
Hacked By AnonymousFox1.0, Coded By AnonymousFox