<p>Forgive me if I'm wrong... by using <code>loop.index0 is divisible by(2)</code> are you trying to determine if the instance is odd or even?</p>

<ul>
<li>1st - returns <code>false</code></li>
<li>2nd - returns <code>true</code></li>
<li>3rd - returns <code>false</code></li>
<li>4th - returns <code>true</code></li>
</ul>

<p>If so Twig does support checking if the loop index is even or odd. Perhaps that would be better to use?</p>

<pre><code>{% if loop.index is even %}Even{% endif %}
</code></pre>

<p><strong>EDIT</strong></p>

<p>I've just had a read of the <a href="https://craftcms.com/docs/templating/switch" rel="noreferrer">Craft docs</a> for the Case and Switch tags and found this:</p>

<blockquote>
  <p>If you’re using the {% switch %} tag inside of a {% for %} loop, you won’t be able to access Twig’s loop variable directly inside of the {% switch %} tag. Instead, you can access it like so:</p>
</blockquote>

<pre><code>{% for matrixBlock in entry.matrixField %}
    {% set loopIndex = loop.index %}

    {% switch matrixBlock.type %}

        {% case "text" %}

            Loop #{{ loopIndex }}

    {% endswitch %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-06-08T15:37:09.043Z
Thanks, yes that is easier but I still have the same problem of it returning the same for all of them
sarah3585
CC BY-SA 3.0
2016-06-08T15:43:52.343Z
@sarah3585 Just updated my answer with some information I found on the Craft docs, perhaps that will resolve your issue?
Jamie Wade
CC BY-SA 3.0
2016-06-08T15:09:48.707Z
2016-06-08T15:43:27.323Z
2016-06-08T15:43:27.323Z
Jamie Wade
<p>What you want is not the <a href="http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable" rel="nofollow">loop variable</a> of the inner loop around the image, but the loop variable of the outer loop. To get to the outer loop, use <code>loop.parent</code>. Try this:</p>

<pre><code>{% if loop.parent.loop.index0 is divisible by(2) %}last{% endif %}
</code></pre>

CC BY-SA 3.0
2016-06-08T16:21:31.373Z
2016-06-08T16:21:31.373Z
Marion Newlevant
<p>Code:</p>

<pre><code>{% for block in entry.testimonials %}
  &lt;div class="col-24 {% if loop.last %}gutter-bottom{% else %}gutter-bottom--none{% endif %}"&gt;
    &lt;div class="section inner"&gt;
      &lt;div class="col-17 {% if loop.index0 is not divisible by(2) %}last float-right {% endif %}"&gt;
        {% switch block.type %}

        {% case "testimonials" %}

          &lt;h3&gt;{{ block.personsName }}&lt;/h3&gt;
          {{ block.quote }}
          &lt;/div&gt;

          {% for asset in block.image %}

            &lt;div class="col-7 circle-image-container {% if loop.index0 is divisible by(2) %}last{% endif %}"&gt;
              &lt;img src="{{ asset.getUrl() }}"  alt="{{ asset.title }}"/&gt;
            &lt;/div&gt;

          {% endfor %}

        {% endswitch %}
    &lt;/div&gt;
  &lt;/div&gt;
{% endfor %}   
</code></pre>

<p>Can anyone point out why the...</p>

<pre><code>loop.index0 is divisible by(2)
</code></pre>

<p>I have on the div around the image isn't working. I'm getting the 'last' class on everything. The one on the previous div is working fine.</p>

<p>What am I missing?</p>

<p>EDIT:</p>

<p>I've just realised it's in it's own image loop causing the problem. How can I get around this?</p>

<loop><index>
Loop divisible by issue
# Loop divisible by issue

(Asked by: username_0 on 2016-06-08)

Code:
{% for block in entry.testimonials %}
  
    
      
        {% switch block.type %}

        {% case "testimonials" %}

          {{ block.personsName }}
          {{ block.quote }}
          

          {% for asset in block.image %}

            
              
            

          {% endfor %}

        {% endswitch %}
    
  
{% endfor %}   

Can anyone point out why the...
loop.index0 is divisible by(2)

I have on the div around the image isn't working. I'm getting the 'last' class on everything. The one on the previous div is working fine.
What am I missing?
EDIT:
I've just realised it's in it's own image loop causing the problem. How can I get around this?

**Tags:** loop, index

---

## 2 answers

### Answer by username_1 on 2016-06-08. Score: 5

Forgive me if I'm wrong... by using loop.index0 is divisible by(2) are you trying to determine if the instance is odd or even?

1st - returns false
2nd - returns true
3rd - returns false
4th - returns true

If so Twig does support checking if the loop index is even or odd. Perhaps that would be better to use?
{% if loop.index is even %}Even{% endif %}

EDIT
I've just had a read of the Craft docs for the Case and Switch tags and found this:

If you’re using the {% switch %} tag inside of a {% for %} loop, you won’t be able to access Twig’s loop variable directly inside of the {% switch %} tag. Instead, you can access it like so:

{% for matrixBlock in entry.matrixField %}
    {% set loopIndex = loop.index %}

    {% switch matrixBlock.type %}

        {% case "text" %}

            Loop #{{ loopIndex }}

    {% endswitch %}
{% endfor %}

> **Comments:**
> **username_0**: Thanks, yes that is easier but I still have the same problem of it returning the same for all of them  
> **username_1**: @sarah3585 Just updated my answer with some information I found on the Craft docs, perhaps that will resolve your issue?  

---

### Answer by username_2 on 2016-06-08. Score: 4

What you want is not the loop variable of the inner loop around the image, but the loop variable of the outer loop. To get to the outer loop, use loop.parent. Try this:
{% if loop.parent.loop.index0 is divisible by(2) %}last{% endif %}


---

