Twig / Swigでこれを行うためのクリーンな方法を知っている人はいますか?
{% for(i = 0; i < 100; i++) %}
blah....
{% endfor %}
数値がある場合は、これを配列に変換してから、タグに 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 ドキュメントは (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 %}
小枝の場合:
{% 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
私は本当に言うことはできませんが、そのdjangoに触発され、djangoにもこの機能がネイティブに欠けているように見えるため、swigではサポートされていない可能性があります: https://code.djangoproject.com/ticket/5172
なのでswigの部分は次回に譲りたいと思います。