Sorry, this seems to be more of a tera question. I noticed that set_global
doesn’t seem to work as I would expect, when used in the context of nested for loops.
For example, consider a simple i
, j
nested for loop, where we’re creating a list for every i
iteration, and then using set_global
to concat to the list within the j
iteration. Like this:
{% for i in [1, 2, 3] %}
{% set list = [] %}
{% for j in [4, 5, 6] %}
{% set_global list = list | concat(with=j) %}
{% endfor %}
{{list}}
{% endfor %}
The output of {{list}}
here is:
[] [] []
This isn’t really what I expect would happen at all. Is this a bug in tera or am I misunderstanding something?
If I move the creation of the list outside of both loops, it behaves as expected. For example:
{% set list = [] %}
{% for i in [1, 2, 3] %}
{% for j in [4, 5, 6] %}
{% set_global list = list | concat(with=j) %}
{% endfor %}
{{list}}
{% endfor %}
Results in:
[4, 5, 6] [4, 5, 6, 4, 5, 6] [4, 5, 6, 4, 5, 6, 4, 5, 6]
Thank you
P.S: There is a workaround, which is to declare the list outside of both loops, and then use set_global
in the outer loop to make the list empty, and then update it within the inner loop. This achieves the same effect, but I was still surprised to see this and think that I have to be misunderstanding something. E.g.
{% set list = [] %}
{% for i in [1, 2, 3] %}
{% set_global list = [] %}
{% for j in [4, 5, 6] %}
{% set_global list = list | concat(with=j) %}
{% endfor %}
{{list}}
{% endfor %}
Makes:
[4, 5, 6] [4, 5, 6] [4, 5, 6]
Which is what I expect