So I've got a Matrix block that is pulling in some entries and I'm looping through the entries in that block in a template. I'm then pulling a text field from each entry and putting it onto a page. Currently, there are three total entries in that block (though that will likely expand over time). Once I hit the third and final entry, I would like the loop to start over again at the first, loop through the entries again until it gets to the last entry and start over again. I'd like to do that continuously up to a point - say 15 loops.

I'm struggling with how to get the entry loop to start over once it reaches the last entry.

This is the current basic code that loops through the entries and then loops through the matrix blocks within the entries to output the text to pull into the page.

{% for entry in block.homeClientQuote %}                                
        {% for block in entry.clientQuote %}
            {{ block.actualClientQuote }}
        {% endfor %} 
{% endfor %}
  • Could you be a little more clear in your question? A thing that might clear things up for yourself is by not mixing block's and entry's as prefixes in your code inside of loops that already use these. It might be confusing and making errors hard to see if you keep using the same name's – Justin Dekkers Mar 1 '17 at 7:59
  • @JustinDekkers Updated the question to make it more clear and good point on the mixing blocks and entries as prefixes. – dpayne Mar 1 '17 at 16:18

You can repeat a loop with another for loop.

For example, here's a loop that repeats 15 times:

{% for i in 1..15 %}
    Loop: {{ i }}<br>
    {% for entry in block.homeClientQuote %}
    {% endfor %}
{% endfor %}

You can also use the i variable if you need to change the output depending on the loop number.

{% if i > 10 %}{% endif %}
| improve this answer | |
  • Tip of the ol' ballcap to ya, Mr. Roper - that did it! Thank you! – dpayne Mar 2 '17 at 0:37

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.