I'm passing an array object from a view in my Flask server to the jinja2 template. Let's say the name is aList
. When I try to change a value inside of aList
like this:
in Flask:
aList = ['a', 'b', 'c']
in the template:
{% set aList[0] = "work, dammit!" %}
I get this error that tells me that "=" is expected instead of "[" in the template.
Can someone tell what the right way of working with arrays is in jinja2?
First: Logic should not be handled in the template!
Second: If you really have to:
If jinja does not accept the array syntax you should be able to work around it by using operator.setitem
from the stdlib. (Be sure to add operator
to globals)
{% set foo = [0, 1, 2, 3, 4] %}
{% set _ = operator.setitem(foo, 'some stuff') %}
{{ foo }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With