[英]Nested Jinja2 for loops
I'm passing 2 variables (3 including a simple one) to some Jinja from a python script. 我正在从python脚本中将2个变量(其中3个包括一个简单变量)传递给某些Jinja。
On certain runs I want to generate a list with links. 在某些运行中,我想生成一个带有链接的列表。
<html>
<body>
<h1>{{ title }}</h1>
<table>
{% if packageURLs is defined %}
{% for x in packageURLs: %}
<a href="{{ x }}">
{% endfor %}
{% for i in packagesList: %}
<tr><td>{{ i }}</td></tr>
{% endfor %}
{" else "}
{% for i in packagesList: %}
<tr><td>{{ i }}</td></tr>
{% endfor %}
{% endif %}
</a>
</table>
</body>
for each 'packageURLs' I want to add a tag then some table tags for 'packages' List. 对于每个“ packageURLs”,我想添加一个标签,然后为“ packages”列表添加一些表标签。 What's happening though is its generating HTML but completing each individual for loop first.
虽然正在发生的是它生成HTML,但是首先完成了每个循环。 So I end up with half a page full of links then another with table rows.
因此,我最后得到的是充满链接的半页,然后是包含表行的另一页。 How can I have it iterate through both initially?
一开始如何遍历两者?
I found a solution: 我找到了解决方案:
<html>
<body>
<h1>{{ title }}</h1>
<table>
{% if packageURLs is defined %}
{% for i,x in packagesList %}
<a href="{{ x }}">
<tr><td>{{ i }}</td></tr>
</a>
{% endfor %}
{" else "}
{% for i in packagesList: %}
<tr><td>{{ i }}</td></tr>
{% endfor %}
{% endif %}
</table>
</body>
Which is populated using zip() in the python script: 这是使用python脚本中的zip()填充的:
f = t.render(title=dataType,packagesList=zip(Info,packageURLs),packageURLs=packageURLs)
I kept the last variable out of laziness as a check since I still want to only execute those different chunks sometimes. 我将最后一个变量保留为懒惰,因为我仍然只想有时只执行那些不同的块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.