Reputation: 365
I am trying to iterate a list to populate a variable to be used to set the value of a hidden field. See my code example below. I am able to iterate the list and concatenate the variable however, when I go to assign the contents of the variable to the hidden input value, there is nothing there. What is the proper method of doing this?
{% set hdnfiles = '' %}
{% if tr.files is not none and tr.files|length > 0 %}
{% for file in tr.files %}
{% if hdnfiles|length > 0 %}
{% set hdnfiles = hdnfiles ~ ";" ~ file %}
{% else %}
{% set hdnfiles = file %}
{% endif %}
{% endfor %}
{% endif %}
<input type="hidden" id="filesHidden" name="filesHidden" value="{{ hdnfiles }}"/>
Upvotes: 0
Views: 125
Reputation: 127220
set
will not override the value on an outer scope. Scoping in Jinja works differently than in Python; control structures (such as if
and for
) have a different scope than their surrounding code (where the initial set
was done). Way back in 2011, Jinja's author stated:
I will keep it in mind for the new compiler backend, but in the current one that is not possible to achieve for performance reasons.
Consider building this string in Python before rendering the template, rather that building it in the template.
Upvotes: 0