Python 你如何索引一个 jinja 模板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20233721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:56:56  来源:igfitidea点击:

How do you index on a jinja template?

pythonjinja2

提问by user986173

I'm passing 3 lists to my jinja template through my python file.

我正在通过我的 python 文件将 3 个列表传递给我的 jinja 模板。

list1 = [1,2,3,4]
list2 = ['a','b','c','d']
list3 = [5,6,7,8]

All these values correspond with eachother, so 1 matches with 'a' and 5, 2 with 'b' and 6, etc.

所有这些值都相互对应,因此 1 与 'a' 和 5 匹配,2 与 'b' 和 6 匹配,依此类推。

In my template I'm printing them out on the same line. How do I do numerical indexing to print them out? As so

在我的模板中,我将它们打印在同一行上。如何进行数字索引以将它们打印出来?就这样

1 a 5
2 b 6
3 c 7

The only thing I know is directly accessing the object through the loop like

我唯一知道的是通过循环直接访问对象

 {%for item in list%}
    {{item}}

回答by Sean Vieira

Two ways:

两种方式:

  1. In your code that calls Jinja simply zipyour lists:

    data = zip(list1, list2, list3)
    # data is now a list of tuples
    # [(1, 'a', 5), (2, 'b', 6), etc.]
    

    Then, in your template you can simply loop over the nested rows:

    {# your_template.jinja #}
    <table>
    {% for row in data %}
        <tr>
        {% for cell in row %}
            <td>{{ cell }}</td>
        {% endfor %}
        </tr>
    {% endfor %}
    </table>
    
  2. As an alternate, if you only want to use Jinja you can use the special loopvariable:

    <table>
    {% for cell in list1 %}
        <tr>
            <td>{{ list1[loop.index0] }}</td>
            <td>{{ list2[loop.index0] }}</td>
            <td>{{ list3[loop.index0] }}</td>
        </tr>
    {% endfor %}
    </table>
    
  1. 在您调用 Jinja 的代码中,您只需zip列出您的列表:

    data = zip(list1, list2, list3)
    # data is now a list of tuples
    # [(1, 'a', 5), (2, 'b', 6), etc.]
    

    然后,在您的模板中,您可以简单地遍历嵌套行:

    {# your_template.jinja #}
    <table>
    {% for row in data %}
        <tr>
        {% for cell in row %}
            <td>{{ cell }}</td>
        {% endfor %}
        </tr>
    {% endfor %}
    </table>
    
  2. 作为替代,如果您只想使用 Jinja,您可以使用特殊loop变量

    <table>
    {% for cell in list1 %}
        <tr>
            <td>{{ list1[loop.index0] }}</td>
            <td>{{ list2[loop.index0] }}</td>
            <td>{{ list3[loop.index0] }}</td>
        </tr>
    {% endfor %}
    </table>
    

回答by Quentin Donnellan

If you really want the index, you could just loop on one of the variables and then uses Jinja's loop.index0feature (returns the current index of the loop starting at 0 (loop.indexdoes the same thing, starting at 1)

如果你真的想要索引,你可以只循环一个变量,然后使用 Jinja 的loop.index0功能(返回循环的当前索引,从 0 开始(loop.index做同样的事情,从 1 开始)

For example:

例如:

{% for item in list1 %}

    {{ item }}
    {{ list2[loop.index0] }}
    {{ list3[loop.index0] }}

{% endfor %}

This assumes your lists are all asserted to be the same length before setting the template, or you'll encounter problems.

这假设您的列表在设置模板之前都被断言为相同的长度,否则您会遇到问题。

回答by Alexander

Similar to @Sean Vieira answer, you can zip the data in your code, then index it in the template. For example:

与@Sean Vieira 的回答类似,您可以在代码中压缩数据,然后在模板中对其进行索引。例如:

data = zip(list1, list2, list3)

data = zip(list1, list2, list3)

<table>
<tr>
       <td>list 1 value</td>
       <td>list 2 value</td>
       <td>list 3 value</td>        
<tr>
{% for row in data %}
<tr>
       <td>{{ row[0] }}</td>
       <td>{{ row[1] }}</td>
       <td>{{ row[2] }}</td>
</tr>
{% endfor %}
</table>