Reputation: 1138
I have a list of values in my template, which I need to increment based on some conditions. Something like this:
{% set samplelist=[0,0,0] %}
{% if condition %}
<p>some text</p>
{% set samplelist[0]=samplelist[0]+listpassedbymainfile[0] %}
{% endif %}
I keep getting this error when I try to run the above code:
TemplateSyntaxError: expected token '=', got '['
Is this not supported, if so, is there a work around ?
Upvotes: 1
Views: 359
Reputation: 156158
Indeed, you can't use jinja in the same way as you would use python. You can, however, unroll your in-place modification with a proper assignment. Note that your list will now be the same from the first element on, but with a different value in its first slot. We can change the assignment to capture the full, new state of samplelist
in that way:
{% set samplelist = [samplelist[0] + listpassedbymainfile[0]] + samplelist[1:] %}
Upvotes: 4