Skip to content
Myr

Merge branch '2.9-maintenance'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parents ad5685f, 78c9612 · Browse files · 1 file+31 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+31 added−0 removed

  • other:+31 added−0 removed

1 file+31 added−0 removed

modified:docs/templates.rst+31 added
672 unmodified lines
673have more than one level of loops, we can rebind the variable `loop` by674writing `{% set outer_loop = loop %}` after the loop that we want to675use recursively. Then, we can call it using `{{ outer_loop(...) }}`676677Please note that assignments in loops will be cleared at the end of the678iteration and cannot outlive the loop scope. Older versions of Jinja2 had679a bug where in some circumstances it appeared that assignments would work.680This is not supported. See :ref:`assignments` for more information about681how to deal with this.682683.. _if:684685If686~~
162 unmodified lines
849850 {% set navigation = [('index.html', 'Index'), ('about.html', 'About')] %}851 {% set key, value = call_something() %}852853.. admonition:: Scoping Behavior854855 Please keep in mind that it is not possible to set variables inside a856 block and have them show up outside of it. This also applies to857 loops. The only exception to that rule are if statements which do not858 introduce a scope. As a result the following template is not going859 to do what you might expect::860861 {% set iterated = false %}862 {% for item in seq %}863 {{ item }}864 {% set iterated = true %}865 {% endfor %}866 {% if not iterated %} did not iterate {% endif %}867868 It is not possible with Jinja syntax to do this. Instead use869 alternative constructs like the loop else block or the special `loop`870 variable::871872 {% for item in seq %}873 {{ item }}874 {% else %}875 did not iterate876 {% endfor %}877878879Block Assignments880~~~~~~~~~~~~~~~~~881882.. versionadded:: 2.8
666 unmodified lines