嵌套列表上的Jinja循环创建空输出

Ste*_*aer 5 python loops jinja2

我有这个结构的列表传递给模板与barsPython 3.4中的名称:

[{'var': 1.18, 'occurrences': [0.0805, 0.0808, 0.0991, 0.0994, 0.2356], 'name': 'item name'},
 {'var': 2.31, 'occurrences': [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791], 'name': 'other name'}]
Run Code Online (Sandbox Code Playgroud)

我希望它为每个字典创建以下输出:

% List with names
item 1: item name
item 2: other name

% List with vars
item 1: 1.18
item 2: 2.31

% List with occurences
item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
Run Code Online (Sandbox Code Playgroud)

前两个没问题,但是我无法让它循环出现列表.我使用以下jinja模板:

{% for item in bars %}
  item {{ loop.index }}: {{ item.name }}
{% endfor %}

{% for item in bars %}
  item {{ loop.index }}: {{ item.var }}
{% endfor %}

{% for item in bars recursive %}
  {% if item.occurrences %}
    Item {{ loop.index}}: {{ loop(item.occurrences) }}
  {% else %}
    No content
  {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

这在第三种情况下给出了这个奇数输出:

Item 1:       No content
No content
No content
No content
No content

Item 2:       No content
No content
No content
No content
No content
No content
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为它似乎循环列表中的每个项目,但它没有通过内容测试.我究竟做错了什么?

编辑:所有三个答案都指向了正确的方向,但@famousgarkin给出了最精细和灵活的答案.我最终得到了以下解决方案:

{% for item in bars %} 
  Item {{ loop.index }}: {% for occurrence in item.occurrences %} subitem {{ loop.index }}: {{ occurrence }} {% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

这让我可以将每个项目放在单独的上下文中,这就是我想要的.但由于这个目标从一开始就不明确,我希望我可以提出你所有的答案.对不起,谢谢大家的快速帮助!

fam*_*kin 3

如果您不想使用完全相同的逻辑来格式化嵌套项目,则不要使用递归。

在您的示例中,当循环bars项目时,它会转到正确的路径,因为您可以Item n:在输出中看到。然后它下降到递归调用来处理item.occurrences项目。现在,当它询问是否occurrences存在时,它实际上是在询问bar.occurrences[i].occurrences[j],当然它没有该occurrences属性,并且它会沿着No content路径走。您可以使用以下命令查看它的实际效果:

{% for item in bars recursive %}
  {% if item.occurrences %}
    Item {{ loop.index }}: {{ loop(item.occurrences) }}
  {% else %}
    No content, {{ item.__class__ }}, {{ item }}
  {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

产量:

Item 1: No content, <type 'float'>, 0.0805
No content, <type 'float'>, 0.0808
No content, <type 'float'>, 0.0991
No content, <type 'float'>, 0.0994
No content, <type 'float'>, 0.2356
...
Run Code Online (Sandbox Code Playgroud)

它将像这样工作,例如:

{% for item in bars %}
  {% if item.occurrences %}
    Item {{ loop.index }}: {{ item.occurrences }}
  {% else %}
    No content
  {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

产量:

Item 1: [0.0805, 0.0808, 0.0991, 0.0994, 0.2356]
Item 2: [1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791]
Run Code Online (Sandbox Code Playgroud)

如果您想自己迭代这些项目以提供自己的格式,您可以使用Jinjajoin过滤器:

{% for item in bars %}
  {% if item.occurrences %}
    Item {{ loop.index }}: {{ item.occurrences|join(', ') }}
  {% else %}
    No content
  {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

产量:

Item 1: 0.0805, 0.0808, 0.0991, 0.0994, 0.2356
Item 2: 1.0859, 1.1121, 1.4826, 1.4829, 1.8126, 1.8791
Run Code Online (Sandbox Code Playgroud)

或者再次循环以滚动完全自定义格式:

{% for item in bars %}
  {% if item.occurrences %}
    Item {{ loop.index }}:
    {% for occurrence in item.occurrences %}
      {{ loop.index }}. {{ occurrence }}
    {% endfor %}
  {% else %}
    No content
  {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

产量:

Item 1:
1. 0.0805
2. 0.0808
3. 0.0991
4. 0.0994
5. 0.2356
...
Run Code Online (Sandbox Code Playgroud)