Drakmail
Drakmail

Reputation: 692

Group output in django template

I need to output something like this:

<div class="scrollable"><div class="items">
    <div><img src="1" alt=""/><img src="2" alt=""/><img src="3" alt=""/></div>
    <div><img src="4" alt=""/><img src="5" alt=""/><img src="6" alt=""/></div>
    <div><img src="7" alt=""/><img src="8" alt=""/><img src="9" alt=""/></div>
</div></div>

from array like ['1','2','3','4','5','6','7','8','9']. How I can do it, given the fact, that array is dynamic?

Upvotes: 1

Views: 88

Answers (1)

Delyan
Delyan

Reputation: 8911

Use the divisibleby built-in filter.

<div>
{% for item in items %}
<img src="{{item}}" />
{% if forloop.counter|divisibleby:"3" %}
    </div> <div>
{% endif %}
{% endfor %}
</div>

Upvotes: 3

Related Questions