使用 Twig 遍历模板中的数组
Loop through array in template with Twig
我有这个数组
{% set icons = { 1: 'glyphicon glyphicon-asterisk',
2: 'glyphicon glyphicon-plus',
3: 'glyphicon glyphicon-minus',
4: 'glyphicon glyphicon-euro'
}
%}
我想遍历它并像这样打印
<li{{ item.attributes.addClass('pos' ~ loop.index, icons[loop.index]) }}>
像这样:
{% for key, value in icons %}
<li{{ item.attributes.addClass('pos' ~ key, value) }}>
{% endfor %}
我有这个数组
{% set icons = { 1: 'glyphicon glyphicon-asterisk',
2: 'glyphicon glyphicon-plus',
3: 'glyphicon glyphicon-minus',
4: 'glyphicon glyphicon-euro'
}
%}
我想遍历它并像这样打印
<li{{ item.attributes.addClass('pos' ~ loop.index, icons[loop.index]) }}>
像这样:
{% for key, value in icons %}
<li{{ item.attributes.addClass('pos' ~ key, value) }}>
{% endfor %}