Here's an elementary loop, which compiles fine when outside of an assignment block:
{% for item in items %}
<p>{{item}}</p>
{% endfor %}
But when I place the loop inside of an assignment block, like so
{% set stuff %}
{% for item in items %}
<p>{{item}}</p>
{% endfor %}
{% endset %}
I get AssertionError: Tried to resolve a name to a reference that was unknown to the frame ('item')
.
The motivation for the question is that I am using macros to avoid code duplication. E.g., I have a number of divs with different fields. One of the divs contains a message to the user. In one (but only one) case, I would like to include a <ul>
in this div, and so I would like to loop through the elements in a list, wrapping each of them in <li>
tags, before passing the resulting html as an argument to the macro. Hence my question.
Is it possible to use a for loop inside of an assignment block? Or is there a better way of achieving the same thing?
See Question&Answers more detail:os