如何迭代jinja中的列表列表

时间:2021-09-25 14:33:51

I have a list of list like :

我有一个列表列表,如:

    [[elem0, elem1, elem2], [elem3, elem4, elem5], [elem6, elem7, elem8], ...]

I wrote the follow template file :

我写了以下模板文件:

    {% for result in results %}
        <tr>
            <td>result[0]</td>
            <td>result[1]</td>
            <td>result[2]</td>
        </tr>
    {% endfor %}

But it didn't work, What i can think is use nested for. Is there another method to access the element in the list in jinja?

但它不起作用,我能想到的是使用嵌套。是否有另一种方法来访问jinja列表中的元素?

1 个解决方案

#1


You still need to output the loop variables inside braces.

您仍然需要在大括号内输出循环变量。

{% for result in results %}
            <tr>
                <td>{{ result[0] }}</td>
                <td>{{ result[1] }}</td>
                <td>{{ result[2] }}</td>
            </tr>
{% endfor %}

Also, consider a nested for loop:

另外,考虑一个嵌套的for循环:

{% for result in results %}
  {% for elem in result %}
    <td>{{elem}}</td>
  {% endfor %}
{% endfor %}

#1


You still need to output the loop variables inside braces.

您仍然需要在大括号内输出循环变量。

{% for result in results %}
            <tr>
                <td>{{ result[0] }}</td>
                <td>{{ result[1] }}</td>
                <td>{{ result[2] }}</td>
            </tr>
{% endfor %}

Also, consider a nested for loop:

另外,考虑一个嵌套的for循环:

{% for result in results %}
  {% for elem in result %}
    <td>{{elem}}</td>
  {% endfor %}
{% endfor %}