<p>You can split an array using the <a href="https://twig.symfony.com/doc/3.x/filters/slice.html" rel="nofollow noreferrer">slice filter</a>. This is useful if you need the two parts in separate places, this way you don't have to iterate through the entire array both times. Slice accepts two arguments, an offset and a length, and returns a new array with the specified number of items starting at the offset.</p>
<pre class="lang-twig prettyprint-override"><code>{% set chunk_1 = array|slice(0, 4) %}
{% set chunk_2 = array|slice(4, 6) %}
</code></pre>

CC BY-SA 4.0
2021-12-17T10:00:33.770Z
Thanks  Moritz. Do you think that will load faster and with less server resources than loop.index?
Markus Busch
CC BY-SA 4.0
2021-12-17T10:14:11.570Z
@MarkusBusch Depends on how you're using the array elements in your template. But honestly, it doesn't matter. With ten array elements, you're talking about miliseconds. Use whichever version is easier to understand and modify in the context of your templates.
MoritzLost
CC BY-SA 4.0
2021-12-17T09:45:00.443Z
2021-12-17T09:45:00.443Z
MoritzLost
<p>You can use Twig's <code>loop.index</code> variable to find out where you are within a for loop.</p>
<p>i.e.</p>
<pre><code>{% for entry in entries %}

    [...code to show entry...]

    {% if loop.index == 4 %}
        [...code to show image...]
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 4.0
2021-12-17T09:56:43.127Z
Works like a charm. Thank you & greetings from Switzerland!
Markus Busch
CC BY-SA 4.0
2021-12-16T19:53:38.377Z
2021-12-16T19:53:38.377Z
stevecomrie
<p>How do I split a list of 10 entries into two parts - one part with 4 entries, followed by an image section, and then the second part with 6 entries.</p>
<p>Thanks for your help. markus</p>

<entry><query>
Split a list of entries in two parts
# Split a list of entries in two parts

(Asked by: username_0 on 2021-12-16)

How do I split a list of 10 entries into two parts - one part with 4 entries, followed by an image section, and then the second part with 6 entries.

Thanks for your help. markus

**Tags:** entry, query


---

## 2 answers

### Answer by username_1 on 2021-12-17. Score: 2

You can split an array using the slice filter. This is useful if you need the two parts in separate places, this way you don't have to iterate through the entire array both times. Slice accepts two arguments, an offset and a length, and returns a new array with the specified number of items starting at the offset.
{% set chunk_1 = array|slice(0, 4) %}
{% set chunk_2 = array|slice(4, 6) %}

> **Comments:**
> **username_0**: Thanks  Moritz. Do you think that will load faster and with less server resources than loop.index?  
> **username_1**: @MarkusBusch Depends on how you're using the array elements in your template. But honestly, it doesn't matter. With ten array elements, you're talking about miliseconds. Use whichever version is easier to understand and modify in the context of your templates.  

---

### Answer by username_2 on 2021-12-16. Score: 0

You can use Twig's loop.index variable to find out where you are within a for loop.
i.e.
{% for entry in entries %}

    [...code to show entry...]

    {% if loop.index == 4 %}
        [...code to show image...]
    {% endif %}

{% endfor %}

> **Comments:**
> **username_0**: Works like a charm. Thank you & greetings from Switzerland!  

---

