From 9574abe564031c7f2034bfce22ed59a39d23948b Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Thu, 12 Jan 2017 16:00:02 +0100 Subject: [PATCH] Added a note for #657 --- docs/templates.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/docs/templates.rst b/docs/templates.rst index fce59c7b..236590f2 100644 --- a/docs/templates.rst +++ b/docs/templates.rst @@ -1515,6 +1515,24 @@ are equivalent:: {{ foo }} {% endwith %} +An important note on scoping here. In Jinja versions before 2.9 the +behavior of referencing one variable to another had some unintended +consequences. In particular one variable could refer to another defined +in the same with block's opening statement. This caused issues with the +cleaned up scoping behavior and has since been improved. In particular +in newer Jinja2 versions the following code always refers to the variable +`a` from outside the `with` block:: + + {% with a={}, b=a.attribute %}...{% endwith %} + +In earlier Jinja versions the `b` attribute would refer to the results of +the first attribute. If you depend on this behavior you can rewrite it to +use the ``set`` tag:: + + {% with a={} %} + {% set b = a.attribute %} + {% endwith %} + .. admonition:: Extension In older versions of Jinja (before 2.9) it was required to enable this -- 2.47.2