6

任何人都知道在 Twig/Swig 中执行此操作的干净方法:

{% for(i = 0; i < 100; i++) %}
    blah....
{% endfor %}
4

3 回答 3

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 %}
于 2013-11-05T15:51:27.690 回答
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 %}

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

于 2013-05-01T21:26:09.947 回答
1

对于树枝它:

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

来自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

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

于 2012-08-24T07:44:50.370 回答