Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-29T19:12:28.147Z
Answers.LastActivityDate: 2023-08-29T19:12:28.147Z
Answers.UserDisplayName: Steve Rowling
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-29T19:10:21.200Z
Answers.LastActivityDate: 2023-08-29T19:10:21.200Z
Answers.UserDisplayName: August Miller
Body: <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>
  
Tags: <craft4><loop><count>
Title: Conditional - Apply After Every Third Loop
ThreadText: # 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 %}
      <!-- AFTER every third loop -->
  {% 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](https://twig.symfony.com/doc/3.x/templates.html#math) (`%`) or the [`divisibleby` test](https://twig.symfony.com/doc/3.x/tests/divisibleby.html)!
  
  Using the remainder or “modulus” operator:
  
  ```
  {% for letter in range('A', 'Z') %}
    <p>Letter {{ letter }} is at position {{ loop.index }}.</p>
  
    {% if loop.index % 3 == 0 %}
      <p>I will only appear every third letter!</p>
    {% endif %}
  {% endfor %}
  
  ```
  
  Using the `divisibleby` test:
  
  ```
  {% for letter in range('A', 'Z') %}
    <p>Letter {{ letter }} is at position {{ loop.index }}.</p>
  
    {% if loop.index is divisibleby(3) %}
      <p>I will only appear every third letter!</p>
    {% endif %}
  {% endfor %}
  
  ```
  
  
  ---
  
  