jinja2 如何在嵌套循环中使用“如果不是最后循环”

问题描述 投票:0回答:1

我有:

a = [a1, a2]
b = [b1, b2]

我想要

{
{a1, b1},
{a1, b2},
{a2, b1},
{a2, b2}
}

我的代码不起作用,因为“如果不是最后循环”考虑第二个循环并抑制太多逗号

{
{% for a in as %}
    {% for b in bs %}
        {a,b} {{"," if not loop last}}
    {% endfor %}
{% endfor %}
}

我明白了

{
{a1, b1},
{a1, b2}
{a2, b1},
{a2, b2}
}

有没有办法告诉jinja2“如果不是最后循环”之类的东西?

jinja2
1个回答
0
投票
{
{% for a in as %}
    {% set outer_loop = loop %}
    {% for b in bs %}
        {a,b} {{"," if not loop last or not outer_loop.last}}
    {% endfor %}
{% endfor %}
}
© www.soinside.com 2019 - 2024. All rights reserved.