我希望能够将当前循环迭代输出到我的模板。
根据the docs https://jinja.palletsprojects.com/en/3.0.x/templates/,有一个loop.counter
我尝试使用的变量:
<ul>
{% for user in userlist %}
<li>
{{ user }} {{loop.counter}}
</li>
{% if loop.counter == 1 %}
This is the First user
{% endif %}
{% endfor %}
</ul>
但正在输出到我的模板。正确的语法是什么?
循环内的计数器变量称为loop.index
在金贾2。
>>> from jinja2 import Template
>>> s = "{% for element in elements %}{{loop.index}} {% endfor %}"
>>> Template(s).render(elements=["a", "b", "c", "d"])
1 2 3 4
此外loop.index
,还有
-
loop.index0
(索引起始于0
)
-
loop.revindex
(反向索引;结束于1
)
-
loop.revindex0
(反向索引;结束于0
)
- 甚至更多在http://jinja.pocoo.org/docs/templates/ http://jinja.pocoo.org/docs/templates/.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)