For loop counter with Twig or Swig

Does anyone know of a clean way to do this in Twig / Swig:

{% for(i = 0; i < 100; i++) %} blah.... {% endfor %} 
+6
source share
3 answers

For a branch:

 {% for i in 0..100 %} * {{ i }} {% endfor %} 

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

For swig, docs don't mention it yet: https://github.com/paularmstrong/swig/blob/master/docs/tags.md#for

I can’t say, but it may not be supported in swig, as its django inspired and django also does not seem to have this nativly function: https://code.djangoproject.com/ticket/5172

so I would like to pass the swig part to the next.

+1
source

If you have a number, you can simply convert it to an array, and then use the Swig standard for the tag. This is easiest if you always want to β€œstart” a loop of 0.

For instance:

 {% set productCount = 6 %} {% set productCountAsArray = Array(productCount) %} {# This will run productCount times #} {% for x, y in productCountAsArray %} This is for number: {{ x }} {% endfor %} 
+14
source

With swig, docs with (ivoba answer) have been updated and now contain special loop variables that include loop.index :

 {% for x in y %} {% if loop.first %}<ul>{% endif %} <li>{{ loop.index }} - {{ loop.key }}: {{ x }}</li> {% if loop.last %}</ul>{% endif %} {% endfor %} 

http://paularmstrong.imtqy.com/swig/docs/#tags-for

+8
source

Source: https://habr.com/ru/post/923614/


All Articles