用于Twig或Swig的循环计数器

cyb*_*bat 6 node.js twig

任何人都知道在Twig/Swig中做到这一点的干净方法:

{% for(i = 0; i < 100; i++) %}
    blah....
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

Ben*_*ett 15

如果您有一个数字,那么您可以将其转换为数组,然后使用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 %}
Run Code Online (Sandbox Code Playgroud)


小智 8

从那以后,swig文档(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 %}
Run Code Online (Sandbox Code Playgroud)

http://paularmstrong.github.io/swig/docs/#tags-for


ivo*_*oba 1

对于树枝来说:

{% for i in 0..100 %}
    * {{ i }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

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

对于 swig,文档尚未提及: https ://github.com/paularmstrong/swig/blob/master/docs/tags.md#for

我真的不能告诉,但 swig 可能不支持它,因为它受到 django 的启发,并且 django 似乎也天生缺乏此功能: https: //code.djangoproject.com/ticket/5172

所以我想把痛饮部分传递给下一个。