问题:如何在python jinja模板中输出loop.counter?
我希望能够将当前循环迭代输出到我的模板。
根据文档:http : //wsgiarea.pocoo.org/jinja/docs/loops.html,我正在尝试使用一个loop.counter变量。
我有以下几点:
<ul>
{% for user in userlist %}
<li>
{{ user }} {{loop.counter}}
</li>
{% if loop.counter == 1 %}
This is the First user
{% endif %}
{% endfor %}
</ul>
虽然没有任何输出到我的模板。正确的语法是什么?
I want to be able to output the current loop iteration to my template.
According to the docs: http://wsgiarea.pocoo.org/jinja/docs/loops.html, there is a loop.counter variable that I am trying to use.
I have the following:
<ul>
{% for user in userlist %}
<li>
{{ user }} {{loop.counter}}
</li>
{% if loop.counter == 1 %}
This is the First user
{% endif %}
{% endfor %}
</ul>
Though nothing is being output to my template. What is the correct syntax?
回答 0
循环内部的计数器变量在jinja2中称为loop.index。
>>> from jinja2 import Template
>>> s = "{% for element in elements %}{{loop.index}} {% endfor %}"
>>> Template(s).render(elements=["a", "b", "c", "d"])
1 2 3 4
有关更多信息,请参见http://jinja.pocoo.org/docs/templates/。
The counter variable inside the loop is called loop.index in jinja2.
>>> from jinja2 import Template
>>> s = "{% for element in elements %}{{loop.index}} {% endfor %}"
>>> Template(s).render(elements=["a", "b", "c", "d"])
1 2 3 4
See http://jinja.pocoo.org/docs/templates/ for more.
回答 1
在for循环块中,您可以访问一些特殊变量,包括loop.index
–but no loop.counter
。从官方文档:
Variable Description
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration.
loop.last True if last iteration.
loop.length The number of items in the sequence.
loop.cycle A helper function to cycle between a list of sequences. See the explanation below.
loop.depth Indicates how deep in a recursive loop the rendering currently is. Starts at level 1
loop.depth0 Indicates how deep in a recursive loop the rendering currently is. Starts at level 0
loop.previtem The item from the previous iteration of the loop. Undefined during the first iteration.
loop.nextitem The item from the following iteration of the loop. Undefined during the last iteration.
loop.changed(*val) True if previously called with a different value (or not called at all).
Inside of a for-loop block, you can access some special variables including loop.index
–but no loop.counter
. From the official docs:
Variable Description
loop.index The current iteration of the loop. (1 indexed)
loop.index0 The current iteration of the loop. (0 indexed)
loop.revindex The number of iterations from the end of the loop (1 indexed)
loop.revindex0 The number of iterations from the end of the loop (0 indexed)
loop.first True if first iteration.
loop.last True if last iteration.
loop.length The number of items in the sequence.
loop.cycle A helper function to cycle between a list of sequences. See the explanation below.
loop.depth Indicates how deep in a recursive loop the rendering currently is. Starts at level 1
loop.depth0 Indicates how deep in a recursive loop the rendering currently is. Starts at level 0
loop.previtem The item from the previous iteration of the loop. Undefined during the first iteration.
loop.nextitem The item from the following iteration of the loop. Undefined during the last iteration.
loop.changed(*val) True if previously called with a different value (or not called at all).
回答 2
如果您使用的是Django,请使用forloop.counter
代替loop.counter
<ul>
{% for user in userlist %}
<li>
{{ user }} {{forloop.counter}}
</li>
{% if forloop.counter == 1 %}
This is the First user
{% endif %}
{% endfor %}
</ul>
if you are using django use forloop.counter
instead of loop.counter
<ul>
{% for user in userlist %}
<li>
{{ user }} {{forloop.counter}}
</li>
{% if forloop.counter == 1 %}
This is the First user
{% endif %}
{% endfor %}
</ul>