Symfony2:如何渲染嵌套的foreach循环并限制内部循环以创建图像库


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

如何创建嵌套循环,然后从上一个循环继续?

示例:

{% 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 %}

我如何使它在第一个.container > .item div中显示4个图像,然后出来创建一个新的.container > .item div并继续,使其成为image[4]

您可以使用batch过滤器:

{% for section in images|batch(4) %}
    <div class="container">
        <div class="item">
            {% for image in section %}
                <img src="{{ image }}" />
            {% endfor %}
        </div>
    </div>
{% endfor %}