我正在获取for循环的索引,并将其传递到宏中
{% for index, sectionForm in forms.sectionalCompletionTabForm.sections %}
{{ macros.projectSectionForm(sectionForm, index) }}
{% endfor %}
然后我想对每个索引进行计数,当然索引将从0开始,所以无论索引是什么,我都需要+1。
{% macro projectSectionForm(form, index) %}
{{ dump(index) }}
{{ dump(index + 1) }}
{% endmacro %}
问题是索引返回为字符串,例如"1"、"2"、"3"、"4"
每当我+1时,我总是得到1,可能是因为它添加了一个int和字符串?
我在网上找过,但找不到任何一种toInt方法。
我的输出看起来像
"0"<lt;索引
1<lt;索引+1
"1"<lt;索引
1<lt;索引+1
"2"
1
http://twig.sensiolabs.org/doc/tags/for.html
如果不需要数组中的索引,则可以使用loop
变量。
loop.index
和look.index0
分别被索引为1和0。
此变量的作用域是存在于for loop
中。
您应该尝试number_format过滤器
{{ dump(index|number_format + 1) }}
http://twig.sensiolabs.org/doc/filters/number_format.html