如何循环遍历 Twig 中的变量序列?
How to loop over sequence with variable in Twig?
根据 Twig's documentation on the "for" tag,..
运算符可用于迭代数字序列:
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
是否可以使用存储上限的变量来代替硬编码的数字?例如,
{% set max = 10 %}
...
{% for i in 0..{{ max }} %}
* {{ i }}
{% endfor %}
当我尝试上面的代码时,我得到这个错误:
A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{".
你应该可以像这样使用变量
{% set max = 10 %}
...
{% for i in 0..max %}
* {{ i }}
{% endfor %}
根据 Twig's documentation on the "for" tag,..
运算符可用于迭代数字序列:
{% for i in 0..10 %}
* {{ i }}
{% endfor %}
是否可以使用存储上限的变量来代替硬编码的数字?例如,
{% set max = 10 %}
...
{% for i in 0..{{ max }} %}
* {{ i }}
{% endfor %}
当我尝试上面的代码时,我得到这个错误:
A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "punctuation" of value "{".
你应该可以像这样使用变量
{% set max = 10 %}
...
{% for i in 0..max %}
* {{ i }}
{% endfor %}