用于Twig或Swig的循环计数器
任何人都知道在Twig / Swig中做到这一点的干净方法:
{% for(i = 0; i < 100; i++) %} blah.... {% endfor %}
对于它的枝杈:
{% for i in 0..100 %} * {{ i }} {% endfor %}
从http://twig.sensiolabs.org/doc/tags/for.html
igig文档不提到它: https : //github.com/paularmstrong/swig/blob/master/docs/tags.md#for
我真的不知道,但它可能不被支持,因为它的Django的启发和django似乎也缺乏这种functionnativly: https ://code.djangoproject.com/ticket/5172
所以我想通过swig部分到下一个。
如果你有一个数字,那么你可以把它转换成一个数组,然后使用Swig的标准标签。 如果你总是想从0开始循环,这是最简单的。
例如:
{% set productCount = 6 %} {% set productCountAsArray = Array(productCount) %} {# This will run productCount times #} {% for x, y in productCountAsArray %} This is for number: {{ x }} {% endfor %}
swig docs自从(ivoba的回答)被更新了,现在包含了special loop variables
,其中包括loop.index
:
{% for x in y %} {% if loop.first %}<ul>{% endif %} <li>{{ loop.index }} - {{ loop.key }}: {{ x }}</li> {% if loop.last %}</ul>{% endif %} {% endfor %}