Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unpack more than one variable on Jinja2

Tags:

python

jinja2

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.

like image 825
Muhammet Can Avatar asked Jul 08 '13 06:07

Muhammet Can


People also ask

How many delimiters are there in Jinja2?

there are two delimiters to split by here: first it's ",", and then the elements themselves are split by ":".

How are variables in Jinja2 templates specified?

Variables. Template variables are defined by the context dictionary passed to the template. You can mess around with the variables in templates provided they are passed in by the application. Variables may have attributes or elements on them you can access too.


1 Answers

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 %}
like image 70
ThiefMaster Avatar answered Oct 18 '22 23:10

ThiefMaster