Server IP : 92.204.138.22 / Your IP : 3.147.86.169 Web Server : Apache System : Linux ns1009439.ip-92-204-138.us 4.18.0-553.8.1.el8_10.x86_64 #1 SMP Tue Jul 2 07:26:33 EDT 2024 x86_64 User : internationaljou ( 1019) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /proc/self/root/proc/thread-self/root/usr/share/doc/python3-jinja2/html/ |
Upload File : |
<!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>Extensions — 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="Integration" href="integration.html" /> <link rel="prev" title="Template Designer Documentation" href="templates.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="integration.html" title="Integration" accesskey="N">next</a> |</li> <li class="right" > <a href="templates.html" title="Template Designer Documentation" 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="extensions"> <span id="jinja-extensions"></span><h1>Extensions<a class="headerlink" href="#extensions" title="Permalink to this headline">¶</a></h1> <p>Jinja2 supports extensions that can add extra filters, tests, globals or even extend the parser. The main motivation of extensions is to move often used code into a reusable class like adding support for internationalization.</p> <div class="section" id="adding-extensions"> <h2>Adding Extensions<a class="headerlink" href="#adding-extensions" title="Permalink to this headline">¶</a></h2> <p>Extensions are added to the Jinja2 environment at creation time. Once the environment is created additional extensions cannot be added. To add an extension pass a list of extension classes or import paths to the <cite>extensions</cite> parameter of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Environment</span></code> constructor. The following example creates a Jinja2 environment with the i18n extension loaded:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">jinja_env</span> <span class="o">=</span> <span class="n">Environment</span><span class="p">(</span><span class="n">extensions</span><span class="o">=</span><span class="p">[</span><span class="s1">'jinja2.ext.i18n'</span><span class="p">])</span> </pre></div> </div> </div> <div class="section" id="i18n-extension"> <span id="id1"></span><h2>i18n Extension<a class="headerlink" href="#i18n-extension" title="Permalink to this headline">¶</a></h2> <p><strong>Import name:</strong> <cite>jinja2.ext.i18n</cite></p> <p>The i18n extension can be used in combination with <a class="reference external" href="https://docs.python.org/dev/library/gettext">gettext</a> or <a class="reference external" href="http://babel.pocoo.org/">babel</a>. If the i18n extension is enabled Jinja2 provides a <cite>trans</cite> statement that marks the wrapped string as translatable and calls <cite>gettext</cite>.</p> <p>After enabling, dummy <cite>_</cite> function that forwards calls to <cite>gettext</cite> is added to the environment globals. An internationalized application then has to provide a <cite>gettext</cite> function and optionally an <cite>ngettext</cite> function into the namespace, either globally or for each rendering.</p> <div class="section" id="environment-methods"> <h3>Environment Methods<a class="headerlink" href="#environment-methods" title="Permalink to this headline">¶</a></h3> <p>After enabling the extension, the environment provides the following additional methods:</p> <dl class="method"> <dt id="jinja2.Environment.install_gettext_translations"> <code class="descclassname">jinja2.Environment.</code><code class="descname">install_gettext_translations</code><span class="sig-paren">(</span><em>translations</em>, <em>newstyle=False</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.Environment.install_gettext_translations" title="Permalink to this definition">¶</a></dt> <dd><p>Installs a translation globally for that environment. The translations object provided must implement at least <cite>ugettext</cite> and <cite>ungettext</cite>. The <cite>gettext.NullTranslations</cite> and <cite>gettext.GNUTranslations</cite> classes as well as <a class="reference external" href="http://babel.pocoo.org/">Babel</a>s <cite>Translations</cite> class are supported.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.5: </span>newstyle gettext added</p> </div> </dd></dl> <dl class="method"> <dt id="jinja2.Environment.install_null_translations"> <code class="descclassname">jinja2.Environment.</code><code class="descname">install_null_translations</code><span class="sig-paren">(</span><em>newstyle=False</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.Environment.install_null_translations" title="Permalink to this definition">¶</a></dt> <dd><p>Install dummy gettext functions. This is useful if you want to prepare the application for internationalization but don’t want to implement the full internationalization system yet.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.5: </span>newstyle gettext added</p> </div> </dd></dl> <dl class="method"> <dt id="jinja2.Environment.install_gettext_callables"> <code class="descclassname">jinja2.Environment.</code><code class="descname">install_gettext_callables</code><span class="sig-paren">(</span><em>gettext</em>, <em>ngettext</em>, <em>newstyle=False</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.Environment.install_gettext_callables" title="Permalink to this definition">¶</a></dt> <dd><p>Installs the given <cite>gettext</cite> and <cite>ngettext</cite> callables into the environment as globals. They are supposed to behave exactly like the standard library’s <code class="xref py py-func docutils literal notranslate"><span class="pre">gettext.ugettext()</span></code> and <code class="xref py py-func docutils literal notranslate"><span class="pre">gettext.ungettext()</span></code> functions.</p> <p>If <cite>newstyle</cite> is activated, the callables are wrapped to work like newstyle callables. See <a class="reference internal" href="#newstyle-gettext"><span class="std std-ref">Whitespace Trimming</span></a> for more information.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> </dd></dl> <dl class="method"> <dt id="jinja2.Environment.uninstall_gettext_translations"> <code class="descclassname">jinja2.Environment.</code><code class="descname">uninstall_gettext_translations</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.Environment.uninstall_gettext_translations" title="Permalink to this definition">¶</a></dt> <dd><p>Uninstall the translations again.</p> </dd></dl> <dl class="method"> <dt id="jinja2.Environment.extract_translations"> <code class="descclassname">jinja2.Environment.</code><code class="descname">extract_translations</code><span class="sig-paren">(</span><em>source</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.Environment.extract_translations" title="Permalink to this definition">¶</a></dt> <dd><p>Extract localizable strings from the given template node or source.</p> <p>For every string found this function yields a <code class="docutils literal notranslate"><span class="pre">(lineno,</span> <span class="pre">function,</span> <span class="pre">message)</span></code> tuple, where:</p> <ul class="simple"> <li><cite>lineno</cite> is the number of the line on which the string was found,</li> <li><cite>function</cite> is the name of the <cite>gettext</cite> function used (if the string was extracted from embedded Python code), and</li> <li><cite>message</cite> is the string itself (a <cite>unicode</cite> object, or a tuple of <cite>unicode</cite> objects for functions with multiple string arguments).</li> </ul> <p>If <a class="reference external" href="http://babel.pocoo.org/">Babel</a> is installed, <a class="reference internal" href="integration.html#babel-integration"><span class="std std-ref">the babel integration</span></a> can be used to extract strings for babel.</p> </dd></dl> <p>For a web application that is available in multiple languages but gives all the users the same language (for example a multilingual forum software installed for a French community) may load the translations once and add the translation methods to the environment at environment generation time:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">translations</span> <span class="o">=</span> <span class="n">get_gettext_translations</span><span class="p">()</span> <span class="n">env</span> <span class="o">=</span> <span class="n">Environment</span><span class="p">(</span><span class="n">extensions</span><span class="o">=</span><span class="p">[</span><span class="s1">'jinja2.ext.i18n'</span><span class="p">])</span> <span class="n">env</span><span class="o">.</span><span class="n">install_gettext_translations</span><span class="p">(</span><span class="n">translations</span><span class="p">)</span> </pre></div> </div> <p>The <cite>get_gettext_translations</cite> function would return the translator for the current configuration. (For example by using <cite>gettext.find</cite>)</p> <p>The usage of the <cite>i18n</cite> extension for template designers is covered as part <a class="reference internal" href="templates.html#i18n-in-templates"><span class="std std-ref">of the template documentation</span></a>.</p> </div> <div class="section" id="whitespace-trimming"> <span id="newstyle-gettext"></span><h3>Whitespace Trimming<a class="headerlink" href="#whitespace-trimming" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <p><span class="versionmodified">New in version 2.10.</span></p> </div> <p>Linebreaks and surrounding whitespace can be automatically trimmed by enabling the <code class="docutils literal notranslate"><span class="pre">ext.i18n.trimmed</span></code> <a class="reference internal" href="api.html#ext-i18n-trimmed"><span class="std std-ref">policy</span></a>.</p> </div> <div class="section" id="id2"> <h3>Newstyle Gettext<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h3> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </div> <p>Starting with version 2.5 you can use newstyle gettext calls. These are inspired by trac’s internal gettext functions and are fully supported by the babel extraction tool. They might not work as expected by other extraction tools in case you are not using Babel’s.</p> <p>What’s the big difference between standard and newstyle gettext calls? In general they are less to type and less error prone. Also if they are used in an autoescaping environment they better support automatic escaping. Here are some common differences between old and new calls:</p> <p>standard gettext:</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="nf">format</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="nf">format</span><span class="o">(</span> <span class="nv">num</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>newstyle gettext looks like this instead:</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>The advantages of newstyle gettext are that you have less to type and that named placeholders become mandatory. The latter sounds like a disadvantage but solves a lot of troubles translators are often facing when they are unable to switch the positions of two placeholder. With newstyle gettext, all format strings look the same.</p> <p>Furthermore with newstyle gettext, string formatting is also used if no placeholders are used which makes all strings behave exactly the same. Last but not least are newstyle gettext calls able to properly mark strings for autoescaping which solves lots of escaping related issues many templates are experiencing over time when using autoescaping.</p> </div> </div> <div class="section" id="expression-statement"> <h2>Expression Statement<a class="headerlink" href="#expression-statement" title="Permalink to this headline">¶</a></h2> <p><strong>Import name:</strong> <cite>jinja2.ext.do</cite></p> <p>The “do” aka expression-statement extension adds a simple <cite>do</cite> tag to the template engine that works like a variable expression but ignores the return value.</p> </div> <div class="section" id="loop-controls"> <span id="loopcontrols-extension"></span><h2>Loop Controls<a class="headerlink" href="#loop-controls" title="Permalink to this headline">¶</a></h2> <p><strong>Import name:</strong> <cite>jinja2.ext.loopcontrols</cite></p> <p>This extension adds support for <cite>break</cite> and <cite>continue</cite> in loops. After enabling, Jinja2 provides those two keywords which work exactly like in Python.</p> </div> <div class="section" id="with-statement"> <span id="with-extension"></span><h2>With Statement<a class="headerlink" href="#with-statement" title="Permalink to this headline">¶</a></h2> <p><strong>Import name:</strong> <cite>jinja2.ext.with_</cite></p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.9.</span></p> </div> <p>This extension is now built-in and no longer does anything.</p> </div> <div class="section" id="autoescape-extension"> <span id="id3"></span><h2>Autoescape Extension<a class="headerlink" href="#autoescape-extension" title="Permalink to this headline">¶</a></h2> <p><strong>Import name:</strong> <cite>jinja2.ext.autoescape</cite></p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.9.</span></p> </div> <p>This extension was removed and is now built-in. Enabling the extension no longer does anything.</p> </div> <div class="section" id="module-jinja2.ext"> <span id="id4"></span><span id="writing-extensions"></span><h2>Writing Extensions<a class="headerlink" href="#module-jinja2.ext" title="Permalink to this headline">¶</a></h2> <p>By writing extensions you can add custom tags to Jinja2. This is a non-trivial task and usually not needed as the default tags and expressions cover all common use cases. The i18n extension is a good example of why extensions are useful. Another one would be fragment caching.</p> <p>When writing extensions you have to keep in mind that you are working with the Jinja2 template compiler which does not validate the node tree you are passing to it. If the AST is malformed you will get all kinds of compiler or runtime errors that are horrible to debug. Always make sure you are using the nodes you create correctly. The API documentation below shows which nodes exist and how to use them.</p> <div class="section" id="example-extension"> <h3>Example Extension<a class="headerlink" href="#example-extension" title="Permalink to this headline">¶</a></h3> <p>The following example implements a <cite>cache</cite> tag for Jinja2 by using the <a class="reference external" href="http://werkzeug.pocoo.org/">Werkzeug</a> caching contrib module:</p> <div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">jinja2</span> <span class="kn">import</span> <span class="n">nodes</span> <span class="kn">from</span> <span class="nn">jinja2.ext</span> <span class="kn">import</span> <span class="n">Extension</span> <span class="k">class</span> <span class="nc">FragmentCacheExtension</span><span class="p">(</span><span class="n">Extension</span><span class="p">):</span> <span class="c1"># a set of names that trigger the extension.</span> <span class="n">tags</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="s1">'cache'</span><span class="p">])</span> <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">environment</span><span class="p">):</span> <span class="nb">super</span><span class="p">(</span><span class="n">FragmentCacheExtension</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">environment</span><span class="p">)</span> <span class="c1"># add the defaults to the environment</span> <span class="n">environment</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span> <span class="n">fragment_cache_prefix</span><span class="o">=</span><span class="s1">''</span><span class="p">,</span> <span class="n">fragment_cache</span><span class="o">=</span><span class="bp">None</span> <span class="p">)</span> <span class="k">def</span> <span class="nf">parse</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span> <span class="c1"># the first token is the token that started the tag. In our case</span> <span class="c1"># we only listen to ``'cache'`` so this will be a name token with</span> <span class="c1"># `cache` as value. We get the line number so that we can give</span> <span class="c1"># that line number to the nodes we create by hand.</span> <span class="n">lineno</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">stream</span><span class="p">)</span><span class="o">.</span><span class="n">lineno</span> <span class="c1"># now we parse a single expression that is used as cache key.</span> <span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="n">parser</span><span class="o">.</span><span class="n">parse_expression</span><span class="p">()]</span> <span class="c1"># if there is a comma, the user provided a timeout. If not use</span> <span class="c1"># None as second parameter.</span> <span class="k">if</span> <span class="n">parser</span><span class="o">.</span><span class="n">stream</span><span class="o">.</span><span class="n">skip_if</span><span class="p">(</span><span class="s1">'comma'</span><span class="p">):</span> <span class="n">args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">parser</span><span class="o">.</span><span class="n">parse_expression</span><span class="p">())</span> <span class="k">else</span><span class="p">:</span> <span class="n">args</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">nodes</span><span class="o">.</span><span class="n">Const</span><span class="p">(</span><span class="bp">None</span><span class="p">))</span> <span class="c1"># now we parse the body of the cache block up to `endcache` and</span> <span class="c1"># drop the needle (which would always be `endcache` in that case)</span> <span class="n">body</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_statements</span><span class="p">([</span><span class="s1">'name:endcache'</span><span class="p">],</span> <span class="n">drop_needle</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span> <span class="c1"># now return a `CallBlock` node that calls our _cache_support</span> <span class="c1"># helper method on this extension.</span> <span class="k">return</span> <span class="n">nodes</span><span class="o">.</span><span class="n">CallBlock</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">call_method</span><span class="p">(</span><span class="s1">'_cache_support'</span><span class="p">,</span> <span class="n">args</span><span class="p">),</span> <span class="p">[],</span> <span class="p">[],</span> <span class="n">body</span><span class="p">)</span><span class="o">.</span><span class="n">set_lineno</span><span class="p">(</span><span class="n">lineno</span><span class="p">)</span> <span class="k">def</span> <span class="nf">_cache_support</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">timeout</span><span class="p">,</span> <span class="n">caller</span><span class="p">):</span> <span class="sd">"""Helper callback."""</span> <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">environment</span><span class="o">.</span><span class="n">fragment_cache_prefix</span> <span class="o">+</span> <span class="n">name</span> <span class="c1"># try to load the block from the cache</span> <span class="c1"># if there is no fragment in the cache, render it and store</span> <span class="c1"># it in the cache.</span> <span class="n">rv</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">environment</span><span class="o">.</span><span class="n">fragment_cache</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span> <span class="k">if</span> <span class="n">rv</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="k">return</span> <span class="n">rv</span> <span class="n">rv</span> <span class="o">=</span> <span class="n">caller</span><span class="p">()</span> <span class="bp">self</span><span class="o">.</span><span class="n">environment</span><span class="o">.</span><span class="n">fragment_cache</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">rv</span><span class="p">,</span> <span class="n">timeout</span><span class="p">)</span> <span class="k">return</span> <span class="n">rv</span> </pre></div> </div> <p>And here is how you use it in an environment:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">jinja2</span> <span class="k">import</span> <span class="n">Environment</span> <span class="kn">from</span> <span class="nn">werkzeug.contrib.cache</span> <span class="k">import</span> <span class="n">SimpleCache</span> <span class="n">env</span> <span class="o">=</span> <span class="n">Environment</span><span class="p">(</span><span class="n">extensions</span><span class="o">=</span><span class="p">[</span><span class="n">FragmentCacheExtension</span><span class="p">])</span> <span class="n">env</span><span class="o">.</span><span class="n">fragment_cache</span> <span class="o">=</span> <span class="n">SimpleCache</span><span class="p">()</span> </pre></div> </div> <p>Inside the template it’s then possible to mark blocks as cacheable. The following example caches a sidebar for 300 seconds:</p> <div class="highlight-html+jinja notranslate"><div class="highlight"><pre><span></span><span class="cp">{%</span> <span class="k">cache</span> <span class="s1">'sidebar'</span><span class="o">,</span> <span class="m">300</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">"sidebar"</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">endcache</span> <span class="cp">%}</span> </pre></div> </div> </div> <div class="section" id="extension-api"> <h3>Extension API<a class="headerlink" href="#extension-api" title="Permalink to this headline">¶</a></h3> <p>Extensions always have to extend the <a class="reference internal" href="#jinja2.ext.Extension" title="jinja2.ext.Extension"><code class="xref py py-class docutils literal notranslate"><span class="pre">jinja2.ext.Extension</span></code></a> class:</p> <dl class="class"> <dt id="jinja2.ext.Extension"> <em class="property">class </em><code class="descclassname">jinja2.ext.</code><code class="descname">Extension</code><span class="sig-paren">(</span><em>environment</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.ext.Extension" title="Permalink to this definition">¶</a></dt> <dd><p>Extensions can be used to add extra functionality to the Jinja template system at the parser level. Custom extensions are bound to an environment but may not store environment specific data on <cite>self</cite>. The reason for this is that an extension can be bound to another environment (for overlays) by creating a copy and reassigning the <cite>environment</cite> attribute.</p> <p>As extensions are created by the environment they cannot accept any arguments for configuration. One may want to work around that by using a factory function, but that is not possible as extensions are identified by their import name. The correct way to configure the extension is storing the configuration values on the environment. Because this way the environment ends up acting as central configuration storage the attributes may clash which is why extensions have to ensure that the names they choose for configuration are not too generic. <code class="docutils literal notranslate"><span class="pre">prefix</span></code> for example is a terrible name, <code class="docutils literal notranslate"><span class="pre">fragment_cache_prefix</span></code> on the other hand is a good name as includes the name of the extension (fragment cache).</p> <dl class="attribute"> <dt id="jinja2.ext.Extension.identifier"> <code class="descname">identifier</code><a class="headerlink" href="#jinja2.ext.Extension.identifier" title="Permalink to this definition">¶</a></dt> <dd><p>The identifier of the extension. This is always the true import name of the extension class and must not be changed.</p> </dd></dl> <dl class="attribute"> <dt id="jinja2.ext.Extension.tags"> <code class="descname">tags</code><a class="headerlink" href="#jinja2.ext.Extension.tags" title="Permalink to this definition">¶</a></dt> <dd><p>If the extension implements custom tags this is a set of tag names the extension is listening for.</p> </dd></dl> <dl class="method"> <dt id="jinja2.ext.Extension.attr"> <code class="descname">attr</code><span class="sig-paren">(</span><em>name</em>, <em>lineno=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.ext.Extension.attr" title="Permalink to this definition">¶</a></dt> <dd><p>Return an attribute node for the current extension. This is useful to pass constants on extensions to generated template code.</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="bp">self</span><span class="o">.</span><span class="n">attr</span><span class="p">(</span><span class="s1">'_my_attribute'</span><span class="p">,</span> <span class="n">lineno</span><span class="o">=</span><span class="n">lineno</span><span class="p">)</span> </pre></div> </div> </dd></dl> <dl class="method"> <dt id="jinja2.ext.Extension.call_method"> <code class="descname">call_method</code><span class="sig-paren">(</span><em>name</em>, <em>args=None</em>, <em>kwargs=None</em>, <em>dyn_args=None</em>, <em>dyn_kwargs=None</em>, <em>lineno=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.ext.Extension.call_method" title="Permalink to this definition">¶</a></dt> <dd><p>Call a method of the extension. This is a shortcut for <a class="reference internal" href="templates.html#attr" title="attr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">attr()</span></code></a> + <a class="reference internal" href="#jinja2.nodes.Call" title="jinja2.nodes.Call"><code class="xref py py-class docutils literal notranslate"><span class="pre">jinja2.nodes.Call</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="jinja2.ext.Extension.filter_stream"> <code class="descname">filter_stream</code><span class="sig-paren">(</span><em>stream</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.ext.Extension.filter_stream" title="Permalink to this definition">¶</a></dt> <dd><p>It’s passed a <a class="reference internal" href="#jinja2.lexer.TokenStream" title="jinja2.lexer.TokenStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">TokenStream</span></code></a> that can be used to filter tokens returned. This method has to return an iterable of <a class="reference internal" href="#jinja2.lexer.Token" title="jinja2.lexer.Token"><code class="xref py py-class docutils literal notranslate"><span class="pre">Token</span></code></a>s, but it doesn’t have to return a <a class="reference internal" href="#jinja2.lexer.TokenStream" title="jinja2.lexer.TokenStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">TokenStream</span></code></a>.</p> <p>In the <cite>ext</cite> folder of the Jinja2 source distribution there is a file called <cite>inlinegettext.py</cite> which implements a filter that utilizes this method.</p> </dd></dl> <dl class="method"> <dt id="jinja2.ext.Extension.parse"> <code class="descname">parse</code><span class="sig-paren">(</span><em>parser</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.ext.Extension.parse" title="Permalink to this definition">¶</a></dt> <dd><p>If any of the <a class="reference internal" href="#jinja2.ext.Extension.tags" title="jinja2.ext.Extension.tags"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tags</span></code></a> matched this method is called with the parser as first argument. The token the parser stream is pointing at is the name token that matched. This method has to return one or a list of multiple nodes.</p> </dd></dl> <dl class="method"> <dt id="jinja2.ext.Extension.preprocess"> <code class="descname">preprocess</code><span class="sig-paren">(</span><em>source</em>, <em>name</em>, <em>filename=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.ext.Extension.preprocess" title="Permalink to this definition">¶</a></dt> <dd><p>This method is called before the actual lexing and can be used to preprocess the source. The <cite>filename</cite> is optional. The return value must be the preprocessed source.</p> </dd></dl> </dd></dl> </div> <div class="section" id="parser-api"> <h3>Parser API<a class="headerlink" href="#parser-api" title="Permalink to this headline">¶</a></h3> <p>The parser passed to <a class="reference internal" href="#jinja2.ext.Extension.parse" title="jinja2.ext.Extension.parse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Extension.parse()</span></code></a> provides ways to parse expressions of different types. The following methods may be used by extensions:</p> <dl class="class"> <dt id="jinja2.parser.Parser"> <em class="property">class </em><code class="descclassname">jinja2.parser.</code><code class="descname">Parser</code><span class="sig-paren">(</span><em>environment</em>, <em>source</em>, <em>name=None</em>, <em>filename=None</em>, <em>state=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.parser.Parser" title="Permalink to this definition">¶</a></dt> <dd><p>This is the central parsing class Jinja2 uses. It’s passed to extensions and can be used to parse expressions or statements.</p> <dl class="attribute"> <dt id="jinja2.parser.Parser.filename"> <code class="descname">filename</code><a class="headerlink" href="#jinja2.parser.Parser.filename" title="Permalink to this definition">¶</a></dt> <dd><p>The filename of the template the parser processes. This is <strong>not</strong> the load name of the template. For the load name see <a class="reference internal" href="#jinja2.parser.Parser.name" title="jinja2.parser.Parser.name"><code class="xref py py-attr docutils literal notranslate"><span class="pre">name</span></code></a>. For templates that were not loaded form the file system this is <cite>None</cite>.</p> </dd></dl> <dl class="attribute"> <dt id="jinja2.parser.Parser.name"> <code class="descname">name</code><a class="headerlink" href="#jinja2.parser.Parser.name" title="Permalink to this definition">¶</a></dt> <dd><p>The load name of the template.</p> </dd></dl> <dl class="attribute"> <dt id="jinja2.parser.Parser.stream"> <code class="descname">stream</code><a class="headerlink" href="#jinja2.parser.Parser.stream" title="Permalink to this definition">¶</a></dt> <dd><p>The current <a class="reference internal" href="#jinja2.lexer.TokenStream" title="jinja2.lexer.TokenStream"><code class="xref py py-class docutils literal notranslate"><span class="pre">TokenStream</span></code></a></p> </dd></dl> <dl class="method"> <dt id="jinja2.parser.Parser.fail"> <code class="descname">fail</code><span class="sig-paren">(</span><em>msg</em>, <em>lineno=None</em>, <em>exc=<class 'jinja2.exceptions.TemplateSyntaxError'></em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.parser.Parser.fail" title="Permalink to this definition">¶</a></dt> <dd><p>Convenience method that raises <cite>exc</cite> with the message, passed line number or last line number as well as the current name and filename.</p> </dd></dl> <dl class="method"> <dt id="jinja2.parser.Parser.free_identifier"> <code class="descname">free_identifier</code><span class="sig-paren">(</span><em>lineno=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.parser.Parser.free_identifier" title="Permalink to this definition">¶</a></dt> <dd><p>Return a new free identifier as <a class="reference internal" href="#jinja2.nodes.InternalName" title="jinja2.nodes.InternalName"><code class="xref py py-class docutils literal notranslate"><span class="pre">InternalName</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="jinja2.parser.Parser.parse_assign_target"> <code class="descname">parse_assign_target</code><span class="sig-paren">(</span><em>with_tuple=True</em>, <em>name_only=False</em>, <em>extra_end_rules=None</em>, <em>with_namespace=False</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.parser.Parser.parse_assign_target" title="Permalink to this definition">¶</a></dt> <dd><p>Parse an assignment target. As Jinja2 allows assignments to tuples, this function can parse all allowed assignment targets. Per default assignments to tuples are parsed, that can be disable however by setting <cite>with_tuple</cite> to <cite>False</cite>. If only assignments to names are wanted <cite>name_only</cite> can be set to <cite>True</cite>. The <cite>extra_end_rules</cite> parameter is forwarded to the tuple parsing function. If <cite>with_namespace</cite> is enabled, a namespace assignment may be parsed.</p> </dd></dl> <dl class="method"> <dt id="jinja2.parser.Parser.parse_expression"> <code class="descname">parse_expression</code><span class="sig-paren">(</span><em>with_condexpr=True</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.parser.Parser.parse_expression" title="Permalink to this definition">¶</a></dt> <dd><p>Parse an expression. Per default all expressions are parsed, if the optional <cite>with_condexpr</cite> parameter is set to <cite>False</cite> conditional expressions are not parsed.</p> </dd></dl> <dl class="method"> <dt id="jinja2.parser.Parser.parse_statements"> <code class="descname">parse_statements</code><span class="sig-paren">(</span><em>end_tokens</em>, <em>drop_needle=False</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.parser.Parser.parse_statements" title="Permalink to this definition">¶</a></dt> <dd><p>Parse multiple statements into a list until one of the end tokens is reached. This is used to parse the body of statements as it also parses template data if appropriate. The parser checks first if the current token is a colon and skips it if there is one. Then it checks for the block end and parses until if one of the <cite>end_tokens</cite> is reached. Per default the active token in the stream at the end of the call is the matched end token. If this is not wanted <cite>drop_needle</cite> can be set to <cite>True</cite> and the end token is removed.</p> </dd></dl> <dl class="method"> <dt id="jinja2.parser.Parser.parse_tuple"> <code class="descname">parse_tuple</code><span class="sig-paren">(</span><em>simplified=False</em>, <em>with_condexpr=True</em>, <em>extra_end_rules=None</em>, <em>explicit_parentheses=False</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.parser.Parser.parse_tuple" title="Permalink to this definition">¶</a></dt> <dd><p>Works like <cite>parse_expression</cite> but if multiple expressions are delimited by a comma a <a class="reference internal" href="#jinja2.nodes.Tuple" title="jinja2.nodes.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a> node is created. This method could also return a regular expression instead of a tuple if no commas where found.</p> <p>The default parsing mode is a full tuple. If <cite>simplified</cite> is <cite>True</cite> only names and literals are parsed. The <cite>no_condexpr</cite> parameter is forwarded to <a class="reference internal" href="#jinja2.parser.Parser.parse_expression" title="jinja2.parser.Parser.parse_expression"><code class="xref py py-meth docutils literal notranslate"><span class="pre">parse_expression()</span></code></a>.</p> <p>Because tuples do not require delimiters and may end in a bogus comma an extra hint is needed that marks the end of a tuple. For example for loops support tuples between <cite>for</cite> and <cite>in</cite>. In that case the <cite>extra_end_rules</cite> is set to <code class="docutils literal notranslate"><span class="pre">['name:in']</span></code>.</p> <p><cite>explicit_parentheses</cite> is true if the parsing was triggered by an expression in parentheses. This is used to figure out if an empty tuple is a valid expression or not.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="jinja2.lexer.TokenStream"> <em class="property">class </em><code class="descclassname">jinja2.lexer.</code><code class="descname">TokenStream</code><span class="sig-paren">(</span><em>generator</em>, <em>name</em>, <em>filename</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream" title="Permalink to this definition">¶</a></dt> <dd><p>A token stream is an iterable that yields <a class="reference internal" href="#jinja2.lexer.Token" title="jinja2.lexer.Token"><code class="xref py py-class docutils literal notranslate"><span class="pre">Token</span></code></a>s. The parser however does not iterate over it but calls <code class="xref py py-meth docutils literal notranslate"><span class="pre">next()</span></code> to go one token ahead. The current active token is stored as <a class="reference internal" href="#jinja2.lexer.TokenStream.current" title="jinja2.lexer.TokenStream.current"><code class="xref py py-attr docutils literal notranslate"><span class="pre">current</span></code></a>.</p> <dl class="attribute"> <dt id="jinja2.lexer.TokenStream.current"> <code class="descname">current</code><a class="headerlink" href="#jinja2.lexer.TokenStream.current" title="Permalink to this definition">¶</a></dt> <dd><p>The current <a class="reference internal" href="#jinja2.lexer.Token" title="jinja2.lexer.Token"><code class="xref py py-class docutils literal notranslate"><span class="pre">Token</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.TokenStream.__next__"> <code class="descname">__next__</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream.__next__" title="Permalink to this definition">¶</a></dt> <dd><p>Go one token ahead and return the old one.</p> <p>Use the built-in <code class="xref py py-func docutils literal notranslate"><span class="pre">next()</span></code> instead of calling this directly.</p> </dd></dl> <dl class="attribute"> <dt id="jinja2.lexer.TokenStream.eos"> <code class="descname">eos</code><a class="headerlink" href="#jinja2.lexer.TokenStream.eos" title="Permalink to this definition">¶</a></dt> <dd><p>Are we at the end of the stream?</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.TokenStream.expect"> <code class="descname">expect</code><span class="sig-paren">(</span><em>expr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream.expect" title="Permalink to this definition">¶</a></dt> <dd><p>Expect a given token type and return it. This accepts the same argument as <a class="reference internal" href="#jinja2.lexer.Token.test" title="jinja2.lexer.Token.test"><code class="xref py py-meth docutils literal notranslate"><span class="pre">jinja2.lexer.Token.test()</span></code></a>.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.TokenStream.look"> <code class="descname">look</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream.look" title="Permalink to this definition">¶</a></dt> <dd><p>Look at the next token.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.TokenStream.next_if"> <code class="descname">next_if</code><span class="sig-paren">(</span><em>expr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream.next_if" title="Permalink to this definition">¶</a></dt> <dd><p>Perform the token test and return the token if it matched. Otherwise the return value is <cite>None</cite>.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.TokenStream.push"> <code class="descname">push</code><span class="sig-paren">(</span><em>token</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream.push" title="Permalink to this definition">¶</a></dt> <dd><p>Push a token back to the stream.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.TokenStream.skip"> <code class="descname">skip</code><span class="sig-paren">(</span><em>n=1</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream.skip" title="Permalink to this definition">¶</a></dt> <dd><p>Got n tokens ahead.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.TokenStream.skip_if"> <code class="descname">skip_if</code><span class="sig-paren">(</span><em>expr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.TokenStream.skip_if" title="Permalink to this definition">¶</a></dt> <dd><p>Like <a class="reference internal" href="#jinja2.lexer.TokenStream.next_if" title="jinja2.lexer.TokenStream.next_if"><code class="xref py py-meth docutils literal notranslate"><span class="pre">next_if()</span></code></a> but only returns <cite>True</cite> or <cite>False</cite>.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="jinja2.lexer.Token"> <em class="property">class </em><code class="descclassname">jinja2.lexer.</code><code class="descname">Token</code><a class="headerlink" href="#jinja2.lexer.Token" title="Permalink to this definition">¶</a></dt> <dd><p>Token class.</p> <dl class="attribute"> <dt id="jinja2.lexer.Token.lineno"> <code class="descname">lineno</code><a class="headerlink" href="#jinja2.lexer.Token.lineno" title="Permalink to this definition">¶</a></dt> <dd><p>The line number of the token</p> </dd></dl> <dl class="attribute"> <dt id="jinja2.lexer.Token.type"> <code class="descname">type</code><a class="headerlink" href="#jinja2.lexer.Token.type" title="Permalink to this definition">¶</a></dt> <dd><p>The type of the token. This string is interned so you may compare it with arbitrary strings using the <cite>is</cite> operator.</p> </dd></dl> <dl class="attribute"> <dt id="jinja2.lexer.Token.value"> <code class="descname">value</code><a class="headerlink" href="#jinja2.lexer.Token.value" title="Permalink to this definition">¶</a></dt> <dd><p>The value of the token.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.Token.test"> <code class="descname">test</code><span class="sig-paren">(</span><em>expr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.Token.test" title="Permalink to this definition">¶</a></dt> <dd><p>Test a token against a token expression. This can either be a token type or <code class="docutils literal notranslate"><span class="pre">'token_type:token_value'</span></code>. This can only test against string values and types.</p> </dd></dl> <dl class="method"> <dt id="jinja2.lexer.Token.test_any"> <code class="descname">test_any</code><span class="sig-paren">(</span><em>*iterable</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.Token.test_any" title="Permalink to this definition">¶</a></dt> <dd><p>Test against multiple token expressions.</p> </dd></dl> </dd></dl> <p>There is also a utility function in the lexer module that can count newline characters in strings:</p> <dl class="function"> <dt id="jinja2.lexer.count_newlines"> <code class="descclassname">jinja2.lexer.</code><code class="descname">count_newlines</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.lexer.count_newlines" title="Permalink to this definition">¶</a></dt> <dd><p>Count the number of newline characters in the string. This is useful for extensions that filter a stream.</p> </dd></dl> </div> <div class="section" id="ast"> <h3>AST<a class="headerlink" href="#ast" title="Permalink to this headline">¶</a></h3> <p>The AST (Abstract Syntax Tree) is used to represent a template after parsing. It’s build of nodes that the compiler then converts into executable Python code objects. Extensions that provide custom statements can return nodes to execute custom Python code.</p> <p>The list below describes all nodes that are currently available. The AST may change between Jinja2 versions but will stay backwards compatible.</p> <p>For more information have a look at the repr of <a class="reference internal" href="api.html#jinja2.Environment.parse" title="jinja2.Environment.parse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">jinja2.Environment.parse()</span></code></a>.</p> <span class="target" id="module-jinja2.nodes"></span><dl class="class"> <dt id="jinja2.nodes.Node"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Node</code><a class="headerlink" href="#jinja2.nodes.Node" title="Permalink to this definition">¶</a></dt> <dd><p>Baseclass for all Jinja2 nodes. There are a number of nodes available of different types. There are four major types:</p> <ul class="simple"> <li><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a>: statements</li> <li><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a>: expressions</li> <li><a class="reference internal" href="#jinja2.nodes.Helper" title="jinja2.nodes.Helper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Helper</span></code></a>: helper nodes</li> <li><a class="reference internal" href="#jinja2.nodes.Template" title="jinja2.nodes.Template"><code class="xref py py-class docutils literal notranslate"><span class="pre">Template</span></code></a>: the outermost wrapper node</li> </ul> <p>All nodes have fields and attributes. Fields may be other nodes, lists, or arbitrary values. Fields are passed to the constructor as regular positional arguments, attributes as keyword arguments. Each node has two attributes: <cite>lineno</cite> (the line number of the node) and <cite>environment</cite>. The <cite>environment</cite> attribute is set at the end of the parsing process for all nodes automatically.</p> <dl class="method"> <dt id="jinja2.nodes.Node.find"> <code class="descname">find</code><span class="sig-paren">(</span><em>node_type</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Node.find" title="Permalink to this definition">¶</a></dt> <dd><p>Find the first node of a given type. If no such node exists the return value is <cite>None</cite>.</p> </dd></dl> <dl class="method"> <dt id="jinja2.nodes.Node.find_all"> <code class="descname">find_all</code><span class="sig-paren">(</span><em>node_type</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Node.find_all" title="Permalink to this definition">¶</a></dt> <dd><p>Find all the nodes of a given type. If the type is a tuple, the check is performed for any of the tuple items.</p> </dd></dl> <dl class="method"> <dt id="jinja2.nodes.Node.iter_child_nodes"> <code class="descname">iter_child_nodes</code><span class="sig-paren">(</span><em>exclude=None</em>, <em>only=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Node.iter_child_nodes" title="Permalink to this definition">¶</a></dt> <dd><p>Iterates over all direct child nodes of the node. This iterates over all fields and yields the values of they are nodes. If the value of a field is a list all the nodes in that list are returned.</p> </dd></dl> <dl class="method"> <dt id="jinja2.nodes.Node.iter_fields"> <code class="descname">iter_fields</code><span class="sig-paren">(</span><em>exclude=None</em>, <em>only=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Node.iter_fields" title="Permalink to this definition">¶</a></dt> <dd><p>This method iterates over all fields that are defined and yields <code class="docutils literal notranslate"><span class="pre">(key,</span> <span class="pre">value)</span></code> tuples. Per default all fields are returned, but it’s possible to limit that to some fields by providing the <cite>only</cite> parameter or to exclude some using the <cite>exclude</cite> parameter. Both should be sets or tuples of field names.</p> </dd></dl> <dl class="method"> <dt id="jinja2.nodes.Node.set_ctx"> <code class="descname">set_ctx</code><span class="sig-paren">(</span><em>ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Node.set_ctx" title="Permalink to this definition">¶</a></dt> <dd><p>Reset the context of a node and all child nodes. Per default the parser will all generate nodes that have a ‘load’ context as it’s the most common one. This method is used in the parser to set assignment targets and other nodes to a store context.</p> </dd></dl> <dl class="method"> <dt id="jinja2.nodes.Node.set_environment"> <code class="descname">set_environment</code><span class="sig-paren">(</span><em>environment</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Node.set_environment" title="Permalink to this definition">¶</a></dt> <dd><p>Set the environment for all nodes.</p> </dd></dl> <dl class="method"> <dt id="jinja2.nodes.Node.set_lineno"> <code class="descname">set_lineno</code><span class="sig-paren">(</span><em>lineno</em>, <em>override=False</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Node.set_lineno" title="Permalink to this definition">¶</a></dt> <dd><p>Set the line numbers of the node and children.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Expr"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Expr</code><a class="headerlink" href="#jinja2.nodes.Expr" title="Permalink to this definition">¶</a></dt> <dd><p>Baseclass for all expressions.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Node" title="jinja2.nodes.Node"><code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code></a></td> </tr> </tbody> </table> <dl class="method"> <dt id="jinja2.nodes.Expr.as_const"> <code class="descname">as_const</code><span class="sig-paren">(</span><em>eval_ctx=None</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Expr.as_const" title="Permalink to this definition">¶</a></dt> <dd><p>Return the value of the expression as constant or raise <a class="reference internal" href="#jinja2.nodes.Impossible" title="jinja2.nodes.Impossible"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Impossible</span></code></a> if this was not possible.</p> <p>An <a class="reference internal" href="api.html#jinja2.nodes.EvalContext" title="jinja2.nodes.EvalContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">EvalContext</span></code></a> can be provided, if none is given a default context is created which requires the nodes to have an attached environment.</p> <div class="versionchanged"> <p><span class="versionmodified">Changed in version 2.4: </span>the <cite>eval_ctx</cite> parameter was added.</p> </div> </dd></dl> <dl class="method"> <dt id="jinja2.nodes.Expr.can_assign"> <code class="descname">can_assign</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Expr.can_assign" title="Permalink to this definition">¶</a></dt> <dd><p>Check if it’s possible to assign something to this node.</p> </dd></dl> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.BinExpr"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">BinExpr</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.BinExpr" title="Permalink to this definition">¶</a></dt> <dd><p>Baseclass for all binary expressions.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Add"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Add</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Add" title="Permalink to this definition">¶</a></dt> <dd><p>Add the left to the right node.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.And"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">And</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.And" title="Permalink to this definition">¶</a></dt> <dd><p>Short circuited AND.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Div"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Div</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Div" title="Permalink to this definition">¶</a></dt> <dd><p>Divides the left by the right node.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.FloorDiv"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">FloorDiv</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.FloorDiv" title="Permalink to this definition">¶</a></dt> <dd><p>Divides the left by the right node and truncates conver the result into an integer by truncating.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Mod"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Mod</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Mod" title="Permalink to this definition">¶</a></dt> <dd><p>Left modulo right.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Mul"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Mul</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Mul" title="Permalink to this definition">¶</a></dt> <dd><p>Multiplies the left with the right node.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Or"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Or</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Or" title="Permalink to this definition">¶</a></dt> <dd><p>Short circuited OR.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Pow"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Pow</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Pow" title="Permalink to this definition">¶</a></dt> <dd><p>Left to the power of right.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Sub"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Sub</code><span class="sig-paren">(</span><em>left</em>, <em>right</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Sub" title="Permalink to this definition">¶</a></dt> <dd><p>Subtract the right from the left node.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.BinExpr" title="jinja2.nodes.BinExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">BinExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Call"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Call</code><span class="sig-paren">(</span><em>node</em>, <em>args</em>, <em>kwargs</em>, <em>dyn_args</em>, <em>dyn_kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Call" title="Permalink to this definition">¶</a></dt> <dd><p>Calls an expression. <cite>args</cite> is a list of arguments, <cite>kwargs</cite> a list of keyword arguments (list of <a class="reference internal" href="#jinja2.nodes.Keyword" title="jinja2.nodes.Keyword"><code class="xref py py-class docutils literal notranslate"><span class="pre">Keyword</span></code></a> nodes), and <cite>dyn_args</cite> and <cite>dyn_kwargs</cite> has to be either <cite>None</cite> or a node that is used as node for dynamic positional (<code class="docutils literal notranslate"><span class="pre">*args</span></code>) or keyword (<code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>) arguments.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Compare"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Compare</code><span class="sig-paren">(</span><em>expr</em>, <em>ops</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Compare" title="Permalink to this definition">¶</a></dt> <dd><p>Compares an expression with some other expressions. <cite>ops</cite> must be a list of <a class="reference internal" href="#jinja2.nodes.Operand" title="jinja2.nodes.Operand"><code class="xref py py-class docutils literal notranslate"><span class="pre">Operand</span></code></a>s.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Concat"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Concat</code><span class="sig-paren">(</span><em>nodes</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Concat" title="Permalink to this definition">¶</a></dt> <dd><p>Concatenates the list of expressions provided after converting them to unicode.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.CondExpr"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">CondExpr</code><span class="sig-paren">(</span><em>test</em>, <em>expr1</em>, <em>expr2</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.CondExpr" title="Permalink to this definition">¶</a></dt> <dd><p>A conditional expression (inline if expression). (<code class="docutils literal notranslate"><span class="pre">{{</span> <span class="pre">foo</span> <span class="pre">if</span> <span class="pre">bar</span> <span class="pre">else</span> <span class="pre">baz</span> <span class="pre">}}</span></code>)</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.ContextReference"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">ContextReference</code><a class="headerlink" href="#jinja2.nodes.ContextReference" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the current template context. It can be used like a <a class="reference internal" href="#jinja2.nodes.Name" title="jinja2.nodes.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> node, with a <code class="docutils literal notranslate"><span class="pre">'load'</span></code> ctx and will return the current <a class="reference internal" href="api.html#jinja2.runtime.Context" title="jinja2.runtime.Context"><code class="xref py py-class docutils literal notranslate"><span class="pre">Context</span></code></a> object.</p> <p>Here an example that assigns the current template name to a variable named <cite>foo</cite>:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Assign</span><span class="p">(</span><span class="n">Name</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">,</span> <span class="n">ctx</span><span class="o">=</span><span class="s1">'store'</span><span class="p">),</span> <span class="n">Getattr</span><span class="p">(</span><span class="n">ContextReference</span><span class="p">(),</span> <span class="s1">'name'</span><span class="p">))</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.EnvironmentAttribute"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">EnvironmentAttribute</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.EnvironmentAttribute" title="Permalink to this definition">¶</a></dt> <dd><p>Loads an attribute from the environment object. This is useful for extensions that want to call a callback stored on the environment.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.ExtensionAttribute"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">ExtensionAttribute</code><span class="sig-paren">(</span><em>identifier</em>, <em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.ExtensionAttribute" title="Permalink to this definition">¶</a></dt> <dd><p>Returns the attribute of an extension bound to the environment. The identifier is the identifier of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Extension</span></code>.</p> <p>This node is usually constructed by calling the <a class="reference internal" href="#jinja2.ext.Extension.attr" title="jinja2.ext.Extension.attr"><code class="xref py py-meth docutils literal notranslate"><span class="pre">attr()</span></code></a> method on an extension.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Filter"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Filter</code><span class="sig-paren">(</span><em>node</em>, <em>name</em>, <em>args</em>, <em>kwargs</em>, <em>dyn_args</em>, <em>dyn_kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Filter" title="Permalink to this definition">¶</a></dt> <dd><p>This node applies a filter on an expression. <cite>name</cite> is the name of the filter, the rest of the fields are the same as for <a class="reference internal" href="#jinja2.nodes.Call" title="jinja2.nodes.Call"><code class="xref py py-class docutils literal notranslate"><span class="pre">Call</span></code></a>.</p> <p>If the <cite>node</cite> of a filter is <cite>None</cite> the contents of the last buffer are filtered. Buffers are created by macros and filter blocks.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Getattr"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Getattr</code><span class="sig-paren">(</span><em>node</em>, <em>attr</em>, <em>ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Getattr" title="Permalink to this definition">¶</a></dt> <dd><p>Get an attribute or item from an expression that is a ascii-only bytestring and prefer the attribute.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Getitem"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Getitem</code><span class="sig-paren">(</span><em>node</em>, <em>arg</em>, <em>ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Getitem" title="Permalink to this definition">¶</a></dt> <dd><p>Get an attribute or item from an expression and prefer the item.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.ImportedName"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">ImportedName</code><span class="sig-paren">(</span><em>importname</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.ImportedName" title="Permalink to this definition">¶</a></dt> <dd><p>If created with an import name the import name is returned on node access. For example <code class="docutils literal notranslate"><span class="pre">ImportedName('cgi.escape')</span></code> returns the <cite>escape</cite> function from the cgi module on evaluation. Imports are optimized by the compiler so there is no need to assign them to local variables.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.InternalName"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">InternalName</code><span class="sig-paren">(</span><em>name</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.InternalName" title="Permalink to this definition">¶</a></dt> <dd><p>An internal name in the compiler. You cannot create these nodes yourself but the parser provides a <a class="reference internal" href="#jinja2.parser.Parser.free_identifier" title="jinja2.parser.Parser.free_identifier"><code class="xref py py-meth docutils literal notranslate"><span class="pre">free_identifier()</span></code></a> method that creates a new identifier for you. This identifier is not available from the template and is not threated specially by the compiler.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Literal"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Literal</code><a class="headerlink" href="#jinja2.nodes.Literal" title="Permalink to this definition">¶</a></dt> <dd><p>Baseclass for literals.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Const"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Const</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Const" title="Permalink to this definition">¶</a></dt> <dd><p>All constant values. The parser will return this node for simple constants such as <code class="docutils literal notranslate"><span class="pre">42</span></code> or <code class="docutils literal notranslate"><span class="pre">"foo"</span></code> but it can be used to store more complex values such as lists too. Only constants with a safe representation (objects where <code class="docutils literal notranslate"><span class="pre">eval(repr(x))</span> <span class="pre">==</span> <span class="pre">x</span></code> is true).</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Literal" title="jinja2.nodes.Literal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Literal</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Dict"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Dict</code><span class="sig-paren">(</span><em>items</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Dict" title="Permalink to this definition">¶</a></dt> <dd><p>Any dict literal such as <code class="docutils literal notranslate"><span class="pre">{1:</span> <span class="pre">2,</span> <span class="pre">3:</span> <span class="pre">4}</span></code>. The items must be a list of <a class="reference internal" href="#jinja2.nodes.Pair" title="jinja2.nodes.Pair"><code class="xref py py-class docutils literal notranslate"><span class="pre">Pair</span></code></a> nodes.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Literal" title="jinja2.nodes.Literal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Literal</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.List"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">List</code><span class="sig-paren">(</span><em>items</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.List" title="Permalink to this definition">¶</a></dt> <dd><p>Any list literal such as <code class="docutils literal notranslate"><span class="pre">[1,</span> <span class="pre">2,</span> <span class="pre">3]</span></code></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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Literal" title="jinja2.nodes.Literal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Literal</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.TemplateData"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">TemplateData</code><span class="sig-paren">(</span><em>data</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.TemplateData" title="Permalink to this definition">¶</a></dt> <dd><p>A constant template 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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Literal" title="jinja2.nodes.Literal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Literal</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Tuple"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Tuple</code><span class="sig-paren">(</span><em>items</em>, <em>ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Tuple" title="Permalink to this definition">¶</a></dt> <dd><p>For loop unpacking and some other things like multiple arguments for subscripts. Like for <a class="reference internal" href="#jinja2.nodes.Name" title="jinja2.nodes.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> <cite>ctx</cite> specifies if the tuple is used for loading the names or storing.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Literal" title="jinja2.nodes.Literal"><code class="xref py py-class docutils literal notranslate"><span class="pre">Literal</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.MarkSafe"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">MarkSafe</code><span class="sig-paren">(</span><em>expr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.MarkSafe" title="Permalink to this definition">¶</a></dt> <dd><p>Mark the wrapped expression as safe (wrap it as <cite>Markup</cite>).</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.MarkSafeIfAutoescape"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">MarkSafeIfAutoescape</code><span class="sig-paren">(</span><em>expr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.MarkSafeIfAutoescape" title="Permalink to this definition">¶</a></dt> <dd><p>Mark the wrapped expression as safe (wrap it as <cite>Markup</cite>) but only if autoescaping is active.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.5.</span></p> </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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Name"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Name</code><span class="sig-paren">(</span><em>name</em>, <em>ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Name" title="Permalink to this definition">¶</a></dt> <dd><p>Looks up a name or stores a value in a name. The <cite>ctx</cite> of the node can be one of the following values:</p> <ul class="simple"> <li><cite>store</cite>: store a value in the name</li> <li><cite>load</cite>: load that name</li> <li><cite>param</cite>: like <cite>store</cite> but if the name was defined as function parameter.</li> </ul> <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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.NSRef"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">NSRef</code><span class="sig-paren">(</span><em>name</em>, <em>attr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.NSRef" title="Permalink to this definition">¶</a></dt> <dd><p>Reference to a namespace value assignment</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Slice"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Slice</code><span class="sig-paren">(</span><em>start</em>, <em>stop</em>, <em>step</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Slice" title="Permalink to this definition">¶</a></dt> <dd><p>Represents a slice object. This must only be used as argument for <code class="xref py py-class docutils literal notranslate"><span class="pre">Subscript</span></code>.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Test"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Test</code><span class="sig-paren">(</span><em>node</em>, <em>name</em>, <em>args</em>, <em>kwargs</em>, <em>dyn_args</em>, <em>dyn_kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Test" title="Permalink to this definition">¶</a></dt> <dd><p>Applies a test on an expression. <cite>name</cite> is the name of the test, the rest of the fields are the same as for <a class="reference internal" href="#jinja2.nodes.Call" title="jinja2.nodes.Call"><code class="xref py py-class docutils literal notranslate"><span class="pre">Call</span></code></a>.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.UnaryExpr"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">UnaryExpr</code><span class="sig-paren">(</span><em>node</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.UnaryExpr" title="Permalink to this definition">¶</a></dt> <dd><p>Baseclass for all unary expressions.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Expr" title="jinja2.nodes.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Neg"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Neg</code><span class="sig-paren">(</span><em>node</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Neg" title="Permalink to this definition">¶</a></dt> <dd><p>Make the expression negative.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.UnaryExpr" title="jinja2.nodes.UnaryExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnaryExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Not"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Not</code><span class="sig-paren">(</span><em>node</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Not" title="Permalink to this definition">¶</a></dt> <dd><p>Negate the expression.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.UnaryExpr" title="jinja2.nodes.UnaryExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnaryExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Pos"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Pos</code><span class="sig-paren">(</span><em>node</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Pos" title="Permalink to this definition">¶</a></dt> <dd><p>Make the expression positive (noop for most expressions)</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.UnaryExpr" title="jinja2.nodes.UnaryExpr"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnaryExpr</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Helper"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Helper</code><a class="headerlink" href="#jinja2.nodes.Helper" title="Permalink to this definition">¶</a></dt> <dd><p>Nodes that exist in a specific context only.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Node" title="jinja2.nodes.Node"><code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Keyword"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Keyword</code><span class="sig-paren">(</span><em>key</em>, <em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Keyword" title="Permalink to this definition">¶</a></dt> <dd><p>A key, value pair for keyword arguments where key is a 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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Helper" title="jinja2.nodes.Helper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Helper</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Operand"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Operand</code><span class="sig-paren">(</span><em>op</em>, <em>expr</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Operand" title="Permalink to this definition">¶</a></dt> <dd><p>Holds an operator and an expression. The following operators are available: <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>, <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>, <code class="docutils literal notranslate"><span class="pre">/</span></code>, <code class="docutils literal notranslate"><span class="pre">eq</span></code>, <code class="docutils literal notranslate"><span class="pre">gt</span></code>, <code class="docutils literal notranslate"><span class="pre">gteq</span></code>, <code class="docutils literal notranslate"><span class="pre">in</span></code>, <code class="docutils literal notranslate"><span class="pre">lt</span></code>, <code class="docutils literal notranslate"><span class="pre">lteq</span></code>, <code class="docutils literal notranslate"><span class="pre">ne</span></code>, <code class="docutils literal notranslate"><span class="pre">not</span></code>, <code class="docutils literal notranslate"><span class="pre">notin</span></code></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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Helper" title="jinja2.nodes.Helper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Helper</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Pair"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Pair</code><span class="sig-paren">(</span><em>key</em>, <em>value</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Pair" title="Permalink to this definition">¶</a></dt> <dd><p>A key, value pair for dicts.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Helper" title="jinja2.nodes.Helper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Helper</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Stmt"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Stmt</code><a class="headerlink" href="#jinja2.nodes.Stmt" title="Permalink to this definition">¶</a></dt> <dd><p>Base node for all statements.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Node" title="jinja2.nodes.Node"><code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Assign"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Assign</code><span class="sig-paren">(</span><em>target</em>, <em>node</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Assign" title="Permalink to this definition">¶</a></dt> <dd><p>Assigns an expression to a target.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.AssignBlock"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">AssignBlock</code><span class="sig-paren">(</span><em>target</em>, <em>filter</em>, <em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.AssignBlock" title="Permalink to this definition">¶</a></dt> <dd><p>Assigns a block to a target.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Block"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Block</code><span class="sig-paren">(</span><em>name</em>, <em>body</em>, <em>scoped</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Block" title="Permalink to this definition">¶</a></dt> <dd><p>A node that represents a block.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Break"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Break</code><a class="headerlink" href="#jinja2.nodes.Break" title="Permalink to this definition">¶</a></dt> <dd><p>Break a loop.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.CallBlock"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">CallBlock</code><span class="sig-paren">(</span><em>call</em>, <em>args</em>, <em>defaults</em>, <em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.CallBlock" title="Permalink to this definition">¶</a></dt> <dd><p>Like a macro without a name but a call instead. <cite>call</cite> is called with the unnamed macro as <cite>caller</cite> argument this node holds.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Continue"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Continue</code><a class="headerlink" href="#jinja2.nodes.Continue" title="Permalink to this definition">¶</a></dt> <dd><p>Continue a loop.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.EvalContextModifier"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">EvalContextModifier</code><span class="sig-paren">(</span><em>options</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.EvalContextModifier" title="Permalink to this definition">¶</a></dt> <dd><p>Modifies the eval context. For each option that should be modified, a <a class="reference internal" href="#jinja2.nodes.Keyword" title="jinja2.nodes.Keyword"><code class="xref py py-class docutils literal notranslate"><span class="pre">Keyword</span></code></a> has to be added to the <code class="xref py py-attr docutils literal notranslate"><span class="pre">options</span></code> list.</p> <p>Example to change the <cite>autoescape</cite> setting:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">EvalContextModifier</span><span class="p">(</span><span class="n">options</span><span class="o">=</span><span class="p">[</span><span class="n">Keyword</span><span class="p">(</span><span class="s1">'autoescape'</span><span class="p">,</span> <span class="n">Const</span><span class="p">(</span><span class="kc">True</span><span class="p">))])</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.ScopedEvalContextModifier"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">ScopedEvalContextModifier</code><span class="sig-paren">(</span><em>options</em>, <em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.ScopedEvalContextModifier" title="Permalink to this definition">¶</a></dt> <dd><p>Modifies the eval context and reverts it later. Works exactly like <a class="reference internal" href="#jinja2.nodes.EvalContextModifier" title="jinja2.nodes.EvalContextModifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">EvalContextModifier</span></code></a> but will only modify the <a class="reference internal" href="api.html#jinja2.nodes.EvalContext" title="jinja2.nodes.EvalContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">EvalContext</span></code></a> for nodes in the <code class="xref py py-attr docutils literal notranslate"><span class="pre">body</span></code>.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.EvalContextModifier" title="jinja2.nodes.EvalContextModifier"><code class="xref py py-class docutils literal notranslate"><span class="pre">EvalContextModifier</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.ExprStmt"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">ExprStmt</code><span class="sig-paren">(</span><em>node</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.ExprStmt" title="Permalink to this definition">¶</a></dt> <dd><p>A statement that evaluates an expression and discards the result.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Extends"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Extends</code><span class="sig-paren">(</span><em>template</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Extends" title="Permalink to this definition">¶</a></dt> <dd><p>Represents an extends statement.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.FilterBlock"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">FilterBlock</code><span class="sig-paren">(</span><em>body</em>, <em>filter</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.FilterBlock" title="Permalink to this definition">¶</a></dt> <dd><p>Node for filter sections.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.For"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">For</code><span class="sig-paren">(</span><em>target</em>, <em>iter</em>, <em>body</em>, <em>else_</em>, <em>test</em>, <em>recursive</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.For" title="Permalink to this definition">¶</a></dt> <dd><p>The for loop. <cite>target</cite> is the target for the iteration (usually a <a class="reference internal" href="#jinja2.nodes.Name" title="jinja2.nodes.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> or <a class="reference internal" href="#jinja2.nodes.Tuple" title="jinja2.nodes.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a>), <cite>iter</cite> the iterable. <cite>body</cite> is a list of nodes that are used as loop-body, and <cite>else_</cite> a list of nodes for the <cite>else</cite> block. If no else node exists it has to be an empty list.</p> <p>For filtered nodes an expression can be stored as <cite>test</cite>, otherwise <cite>None</cite>.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.FromImport"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">FromImport</code><span class="sig-paren">(</span><em>template</em>, <em>names</em>, <em>with_context</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.FromImport" title="Permalink to this definition">¶</a></dt> <dd><p>A node that represents the from import tag. It’s important to not pass unsafe names to the name attribute. The compiler translates the attribute lookups directly into getattr calls and does <em>not</em> use the subscript callback of the interface. As exported variables may not start with double underscores (which the parser asserts) this is not a problem for regular Jinja code, but if this node is used in an extension extra care must be taken.</p> <p>The list of names may contain tuples if aliases are wanted.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.If"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">If</code><span class="sig-paren">(</span><em>test</em>, <em>body</em>, <em>elif_</em>, <em>else_</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.If" title="Permalink to this definition">¶</a></dt> <dd><p>If <cite>test</cite> is true, <cite>body</cite> is rendered, else <cite>else_</cite>.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Import"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Import</code><span class="sig-paren">(</span><em>template</em>, <em>target</em>, <em>with_context</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Import" title="Permalink to this definition">¶</a></dt> <dd><p>A node that represents the import tag.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Include"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Include</code><span class="sig-paren">(</span><em>template</em>, <em>with_context</em>, <em>ignore_missing</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Include" title="Permalink to this definition">¶</a></dt> <dd><p>A node that represents the include tag.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Macro"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Macro</code><span class="sig-paren">(</span><em>name</em>, <em>args</em>, <em>defaults</em>, <em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Macro" title="Permalink to this definition">¶</a></dt> <dd><p>A macro definition. <cite>name</cite> is the name of the macro, <cite>args</cite> a list of arguments and <cite>defaults</cite> a list of defaults if there are any. <cite>body</cite> is a list of nodes for the macro body.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Output"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Output</code><span class="sig-paren">(</span><em>nodes</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Output" title="Permalink to this definition">¶</a></dt> <dd><p>A node that holds multiple expressions which are then printed out. This is used both for the <cite>print</cite> statement and the regular template data.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.OverlayScope"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">OverlayScope</code><span class="sig-paren">(</span><em>context</em>, <em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.OverlayScope" title="Permalink to this definition">¶</a></dt> <dd><p>An overlay scope for extensions. This is a largely unoptimized scope that however can be used to introduce completely arbitrary variables into a sub scope from a dictionary or dictionary like object. The <cite>context</cite> field has to evaluate to a dictionary object.</p> <p>Example usage:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">OverlayScope</span><span class="p">(</span><span class="n">context</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">call_method</span><span class="p">(</span><span class="s1">'get_context'</span><span class="p">),</span> <span class="n">body</span><span class="o">=</span><span class="p">[</span><span class="o">...</span><span class="p">])</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified">New in version 2.10.</span></p> </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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Scope"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Scope</code><span class="sig-paren">(</span><em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Scope" title="Permalink to this definition">¶</a></dt> <dd><p>An artificial scope.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.With"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">With</code><span class="sig-paren">(</span><em>targets</em>, <em>values</em>, <em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.With" title="Permalink to this definition">¶</a></dt> <dd><p>Specific node for with statements. In older versions of Jinja the with statement was implemented on the base of the <cite>Scope</cite> node instead.</p> <div class="versionadded"> <p><span class="versionmodified">New in version 2.9.3.</span></p> </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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Stmt" title="jinja2.nodes.Stmt"><code class="xref py py-class docutils literal notranslate"><span class="pre">Stmt</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="class"> <dt id="jinja2.nodes.Template"> <em class="property">class </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Template</code><span class="sig-paren">(</span><em>body</em><span class="sig-paren">)</span><a class="headerlink" href="#jinja2.nodes.Template" title="Permalink to this definition">¶</a></dt> <dd><p>Node that represents a template. This must be the outermost node that is passed to the compiler.</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">Node type:</th><td class="field-body"><a class="reference internal" href="#jinja2.nodes.Node" title="jinja2.nodes.Node"><code class="xref py py-class docutils literal notranslate"><span class="pre">Node</span></code></a></td> </tr> </tbody> </table> </dd></dl> <dl class="exception"> <dt id="jinja2.nodes.Impossible"> <em class="property">exception </em><code class="descclassname">jinja2.nodes.</code><code class="descname">Impossible</code><a class="headerlink" href="#jinja2.nodes.Impossible" title="Permalink to this definition">¶</a></dt> <dd><p>Raised if the node could not perform a requested action.</p> </dd></dl> </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="#">Extensions</a><ul> <li><a class="reference internal" href="#adding-extensions">Adding Extensions</a></li> <li><a class="reference internal" href="#i18n-extension">i18n Extension</a><ul> <li><a class="reference internal" href="#environment-methods">Environment Methods</a></li> <li><a class="reference internal" href="#whitespace-trimming">Whitespace Trimming</a></li> <li><a class="reference internal" href="#id2">Newstyle Gettext</a></li> </ul> </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> <li><a class="reference internal" href="#autoescape-extension">Autoescape Extension</a></li> <li><a class="reference internal" href="#module-jinja2.ext">Writing Extensions</a><ul> <li><a class="reference internal" href="#example-extension">Example Extension</a></li> <li><a class="reference internal" href="#extension-api">Extension API</a></li> <li><a class="reference internal" href="#parser-api">Parser API</a></li> <li><a class="reference internal" href="#ast">AST</a></li> </ul> </li> </ul> </li> </ul> <h3>Related Topics</h3> <ul> <li><a href="index.html">Documentation overview</a><ul> <li>Previous: <a href="templates.html" title="previous chapter">Template Designer Documentation</a></li> <li>Next: <a href="integration.html" title="next chapter">Integration</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/extensions.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>