duancai1904 2015-11-23 13:52
浏览 61
已采纳

Twig索引类型为字符串

I am grabbing the index of a for loop and passing it into a macro

{% for index, sectionForm in forms.sectionalCompletionTabForm.sections %}

    {{ macros.projectSectionForm(sectionForm, index) }}
{% endfor %}

I am then wanting to count up with each index, ofcourse the index will start on 0 so I need to + 1 to whatever the index is.

{% macro projectSectionForm(form, index) %}

 {{ dump(index) }}
 {{ dump(index + 1) }}

{% endmacro %}

The problem is that index is coming back as a string eg. "1","2","3","4"

and whenever I + 1 to it i will always get 1 , possibly because it is adding a int and string?

I have looked on-line and I have not being able to find any kind of a toInt method.

My output looks like

"0" << index
1 << index + 1
"1"<< index
1 << index + 1
"2"
1

  • 写回答

2条回答 默认 最新

  • dsj60862 2015-11-23 14:20
    关注

    http://twig.sensiolabs.org/doc/tags/for.html

    If you don't need the index from the array, then you can use the loop variable.

    loop.index and look.index0 are 1 indexed, and 0 indexed respectively.

    This variable is scoped to exist within the for loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line