Adding Code or Content Every X Entries

Problem

When looping through entries, you want to do something different every x items.

Solution

When looping through entries, use the divisibleby Twig test on the current loop.index to adjust the output every x items.

For example, the following code adds some additional markup every 3 entries.

{% if loop.index is divisibleby(3) %}
    <div class="clearfix"></div>
{% endif %}

Discussion

The Twig documentation contains comprehensive information about both the loop.index variable, and the divisibleby test.