[英]working with arrays in jinja2
I'm passing an array object from a view in my Flask server to the jinja2 template. 我正在将数组对象从Flask服务器中的视图传递到jinja2模板。 Let's say the name is
aList
. 假设名称为
aList
。 When I try to change a value inside of aList
like this: 当我尝试像这样更改
aList
内的值时:
in Flask: 在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? 有人可以说在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. 如果jinja不接受数组语法,则应该可以使用stdlib中的
operator.setitem
来解决它。 (Be sure to add operator
to globals) (确保将
operator
添加到全局变量)
{% set foo = [0, 1, 2, 3, 4] %}
{% set _ = operator.setitem(foo, 'some stuff') %}
{{ foo }}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.