如何在Jinja2上解压多个变量 [英] How to unpack more than one variable on Jinja2

查看:88
本文介绍了如何在Jinja2上解压多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Jinja模板引擎上解压缩多个变量.我该如何实现?

I'm trying to unpack more than one variable on jinja template engine. How can I achieve this?

我正在努力实现这样的目标;

I'm trying to achieve something like this;

{% for item1, item2, item3 in items %}
<div class="row">
  <div class="four columns">
    <img src="static{{ item1.pics.0 }}" class="picitem" alt=""/>
  </div>

  <div class="four columns">
    <img src="static{{ item2.pics.0 }}" class="picitem" alt="" />
  </div>

  <div class="four columns">
    <img src="static{{ item3.pics.0 }}" class="picitem" alt=""/>      
  </div>
</div>
{% endfor %}

这显然不能通过给予来实现;

This is obviously not working by giving;

ValueError: too many values to unpack

任何想法都会受到赞赏.

Any ideas would be appreciated.

推荐答案

使用 batch过滤器遍历大块:

Use the batch filter to iterate over chunks:

{% for tmp in items|batch(3) %}
  <div class="row">
    {% for item in tmp %}
      <div class="four columns">
        <img src="static{{ item.pics.0 }}" class="picitem" alt=""/>
      </div>
    {% endfor %}
  </div>
{% endfor %}

这篇关于如何在Jinja2上解压多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆