<p>There is the <code>divisible by()</code> test in Twig that tests for a number being divisible by another. Twig also has an alternative to <code>loop.index</code> called <code>loop.index0</code> that starts at <code>0</code> instead of <code>1</code>. You can combine these like this to output something very third iteration of the loop:</p>
<pre><code>{% if loop.index0 is divisible by(3) %}
    ...
{% endif %}
</code></pre>

CC BY-SA 4.0
2023-08-29T19:12:28.147Z
2023-08-29T19:12:28.147Z
Steve Rowling
<p>You're looking for the <a href="https://twig.symfony.com/doc/3.x/templates.html#math" rel="nofollow noreferrer">remainder operator</a> (<code>%</code>) or the <a href="https://twig.symfony.com/doc/3.x/tests/divisibleby.html" rel="nofollow noreferrer"><code>divisibleby</code> test</a>!</p>
<p>Using the remainder or “modulus” operator:</p>
<pre><code>{% for letter in range('A', 'Z') %}
  &lt;p&gt;Letter {{ letter }} is at position {{ loop.index }}.&lt;/p&gt;

  {% if loop.index % 3 == 0 %}
    &lt;p&gt;I will only appear every third letter!&lt;/p&gt;
  {% endif %}
{% endfor %}
</code></pre>
<p>Using the <code>divisibleby</code> test:</p>
<pre><code>{% for letter in range('A', 'Z') %}
  &lt;p&gt;Letter {{ letter }} is at position {{ loop.index }}.&lt;/p&gt;

  {% if loop.index is divisibleby(3) %}
    &lt;p&gt;I will only appear every third letter!&lt;/p&gt;
  {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 4.0
2023-08-29T19:10:21.200Z
2023-08-29T19:10:21.200Z
August Miller
<p>The following is messy and obviously not scalable. There must be a way in twig to target a common difference of 3 right?</p>
<pre><code>{% if loop.first or loop.index == 4 or loop.index == 7 or loop.index == 10 %}
    &lt;!-- AFTER every third loop --&gt;
{% endif %}
</code></pre>

<craft4><loop><count>
Conditional - Apply After Every Third Loop
# Conditional - Apply After Every Third Loop

(Asked by: username_0 on 2023-08-29)

The following is messy and obviously not scalable. There must be a way in twig to target a common difference of 3 right?
{% if loop.first or loop.index == 4 or loop.index == 7 or loop.index == 10 %}
    
{% endif %}

**Tags:** craft4, loop, count


---

## 2 answers

### Answer by username_1 on 2023-08-29. Score: 1 | Accepted answer

There is the divisible by() test in Twig that tests for a number being divisible by another. Twig also has an alternative to loop.index called loop.index0 that starts at 0 instead of 1. You can combine these like this to output something very third iteration of the loop:
{% if loop.index0 is divisible by(3) %}
    ...
{% endif %}


---

### Answer by username_2 on 2023-08-29. Score: 1

You're looking for the remainder operator (%) or the divisibleby test!
Using the remainder or “modulus” operator:
{% for letter in range('A', 'Z') %}
  Letter {{ letter }} is at position {{ loop.index }}.

  {% if loop.index % 3 == 0 %}
    I will only appear every third letter!
  {% endif %}
{% endfor %}

Using the divisibleby test:
{% for letter in range('A', 'Z') %}
  Letter {{ letter }} is at position {{ loop.index }}.

  {% if loop.index is divisibleby(3) %}
    I will only appear every third letter!
  {% endif %}
{% endfor %}


---

