<p>Found this nugget about <a href="https://twig.symfony.com/doc/2.x/tags/for.html#iterating-over-a-subset" rel="nofollow noreferrer">Iterating over a Subset</a> in the Twig docs about slice:</p>

<pre><code>{% for client in clients|slice(0, 8) %}
    ... 
{% endfor %}

{% for client in clients|slice(8) %}
      ...
{% endfor %}
</code></pre>

<p>More details about <a href="https://twig.symfony.com/doc/2.x/filters/slice.html" rel="nofollow noreferrer">slice</a>.</p>

CC BY-SA 3.0
2017-10-13T16:26:43.997Z
2017-10-13T16:26:43.997Z
RitterKnight
<p>Basically, I have two sections of a page. In one section of the page, I want to output the first 8 entries in a loop. In the next section of a page, I want to output from the 9th entry to the end of the loop. The entries that I'm looping through are eager loaded.</p>

<p>This is how I'm doing it right now:</p>

<pre><code>{% set clients = element.clients.find({
    with: [
        ['logo', {
            withTransforms: ['newClientLogoPage']
        },]
    ]})
%}

SECTION #1:
{% for client in clients %}   
    {% if loop.index0 &lt;= 7 %}
        DO SOME STUFF

SECTION #2:
{% for client in clients %} 
    {% if loop.index0 &gt;= 8 %} 
        DO SOME STUFF
</code></pre>

<p>Essentially, I'm fully looping through the same entries twice and just starting and ending what is output at different spots using loop index.</p>

<p>Is there a better / more efficient way to start and end my loops at different points without doing two full loops like I am now?</p>

<forloop><loop><eager-loading>
Starting & Ending Loops with Eager Loaded Elements
# Starting & Ending Loops with Eager Loaded Elements

(Asked by: username_0 on 2017-10-12)

Basically, I have two sections of a page. In one section of the page, I want to output the first 8 entries in a loop. In the next section of a page, I want to output from the 9th entry to the end of the loop. The entries that I'm looping through are eager loaded.
This is how I'm doing it right now:
{% set clients = element.clients.find({
    with: [
        ['logo', {
            withTransforms: ['newClientLogoPage']
        },]
    ]})
%}

SECTION #1:
{% for client in clients %}   
    {% if loop.index0 <= 7 %}
        DO SOME STUFF

SECTION #2:
{% for client in clients %} 
    {% if loop.index0 >= 8 %} 
        DO SOME STUFF

Essentially, I'm fully looping through the same entries twice and just starting and ending what is output at different spots using loop index.
Is there a better / more efficient way to start and end my loops at different points without doing two full loops like I am now?

**Tags:** forloop, loop, eager-loading


---

## 1 answers

### Answer by username_1 on 2017-10-13. Score: 2 | Accepted answer

Found this nugget about Iterating over a Subset in the Twig docs about slice:
{% for client in clients|slice(0, 8) %}
    ... 
{% endfor %}

{% for client in clients|slice(8) %}
      ...
{% endfor %}

More details about slice.


---

