There is a normal cycle
{% for i in x %} {{loop.index0}} <br> {% endfor %} Displays 0 1 2. How to make it display 2 1 0?
There is a normal cycle
{% for i in x %} {{loop.index0}} <br> {% endfor %} Displays 0 1 2. How to make it display 2 1 0?
{{loop.index0}} is the current iteration of the loop, starting from zero, so this variable will not give you 2,1,0
and to get 2,1,0 you need to write this:
{% for i in x %} {{loop.revindex0}} <br> {% endfor %} if you need to output variables in reverse order, then you need to use reverse :
{% for i in x | reverse %} {{ i }} <br> {% endfor %} Read the official documentation, there are many useful things:
http://twig.sensiolabs.org/doc/2.x/tags/for.html
http://twig.sensiolabs.org/doc/2.x/filters/reverse.html
Source: https://ru.stackoverflow.com/questions/614355/
All Articles