For loops create another stack frame where the value set is local to that loop, that’s a classic gotcha of Jinja2.
If you want to update a value outside of the for loop, you need to use set_global
But I’m not updating the value outside the loop nor am I printing the value outside the loop.
I have {% set var = 2 -%} inside the for loop after print statement (please check source code posted above). I am curious to know why the print statement in for loop is not picking up that set statement (which itself is inside the loop).
As pointed out in subsequent comment, the value does get picked up if the order of those 2 statements are reversed. What difference does it make? Both statements are still inside the loop.
{% set var = 1.7 %}
{% set array = [15, "true", 1.7, 33, 26,] %}
{% for i in array %}
{{ i ~ " " ~ var -}} <-- this looks for a `var` in the loop, doesn't find it and then looks up in the global context
{% set var = 2 -%} <-- this sets a variable in the current iteration (NOT the whole for loop). Since there is nothing after, this is essentially a no-op as this context gets cleared for the next loop
{% endfor %}
If you invert it, then the concatenation will find the var value of the iteration (2) since it’s set rather than 1.7