1

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 Answers 2

1

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
  • Great Answer Thank you for the help!
    – jmack
    Nov 26, 2015 at 17:13
0

you should try the number_format filter

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

http://twig.sensiolabs.org/doc/filters/number_format.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.