Sygon
Sygon

Reputation: 222

Symfony2: How to render nested foreach loops and limit the inner loop to create an image gallery?

How can I have a nested loop and then carry on from the previous loop?

Example:

{% for image in site_images[page.id].images %}
<div class="container">
    <div class="item">
        <img src="{{ image[loop.index0] }}" />
        <img src="{{ image[loop.index0] }}" />
        <img src="{{ image[loop.index0] }}" />
        <img src="{{ image[loop.index0] }}" />
    </div>
</div>
<div class="container">
    <div class="item">
        <img src="{{ image[loop.index0] }}" />
        <img src="{{ image[loop.index0] }}" />
        <img src="{{ image[loop.index0] }}" />
        <img src="{{ image[loop.index0] }}" />
    </div>
</div>
....
{% endfor %}

How can I make it show 4 images in the first .container > .item div, then come out create a new .container > .item div and carry on so it would be image[4]

Upvotes: 2

Views: 987

Answers (1)

Nicolai Fr&#246;hlich
Nicolai Fr&#246;hlich

Reputation: 52513

You can use the batch filter:

{% for section in images|batch(4) %}

    <div class="container">
        <div class="item">

            {% for image in section %}
                <img src="{{ image }}" />
            {% endfor %}

        </div>
    </div>

{% endfor %}

Upvotes: 4

Related Questions