1

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?

2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.