<p>To better understand, do you want transform for all <strong>even</strong> iterations on the array? If so, Twig's <a href="http://twig.sensiolabs.org/doc/functions/cycle.html" rel="nofollow"><code>cycle</code></a> function should do what you want:</p>

<pre><code>{% set asset = entry.portraits %}
{% if asset | length %}
   {% for asset in entry.portraits %}
      {% set transform = cycle(['largeIndex', 'largeIndexHalf'], loop.index0) %}
      &lt;div class="item"&gt;&lt;img src="{{ asset.getUrl(transform) }}" /&gt;&lt;/div&gt;
   {% endfor %}
{% endif %}
</code></pre>

<p><strong>Edit</strong></p>

<p>Rereading the question and Brandon's answer, I may have misinterpreted that you do indeed <em>just</em> want the 2nd and 4th image. I'll leave my answer for posterity for people looking to do an even / odd thing though.</p>

CC BY-SA 3.0
2015-03-12T16:30:03.567Z
2015-03-12T16:59:23.950Z
2015-03-12T16:59:23.950Z
Peter Tell
<p>You could do that like so:</p>

<pre><code>{% for asset in entry.portraits %}
    {% if loop.index == 2 or loop.index == 4 %}
        {% set transform = 'largeIndexHalf' %}
    {% else %}
        {% set transform = 'largeIndex' %}
    {% endif %}
    &lt;div class="item"&gt;&lt;img src="{{ asset.getUrl(transform) }}" /&gt;&lt;/div&gt;
{% endfor %}
</code></pre>

<p>or more simply (making use of the <a href="http://twig.sensiolabs.org/doc/templates.html#containment-operator">containment operator</a> (<code>in</code>) and the ternary operator (<code>? :</code>):</p>

<pre><code>{% for asset in entry.portraits %}
    {% set transform = loop.index in [2, 4] ? 'largeIndexHalf' : 'largeIndex' %}
    &lt;div class="item"&gt;&lt;img src="{{ asset.getUrl(transform) }}" /&gt;&lt;/div&gt;
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-03-12T21:49:55.580Z
Works a treat. Thank you. Example here: http://alisilversteinportraits.com/
Niall Thompson
CC BY-SA 3.0
2015-03-12T16:29:24.137Z
2015-03-12T16:29:24.137Z
Brandon Kelly
<p>I have a masonry type image grid layout and I need to specify a different image transform for just the 2nd and fourth images in this grid.</p>

<p>Does the image ID relate to the order or to the specific image? How could I include a conditional in my code to output the largeIndexHalf image transform for the 2nd and 4th image?</p>

<pre><code>{% set asset = entry.portraits %}
{% if asset | length %}
{% for asset in entry.portraits %}
&lt;div class="item"&gt;&lt;img src="{{ asset.getUrl('largeIndex') }}" /&gt;&lt;/div&gt;
{% endfor %}
{% endif %}
</code></pre>

<assets><image-transforms>
Transform different image if specific id or order
# Transform different image if specific id or order

(Asked by: username_0 on 2015-03-12)

I have a masonry type image grid layout and I need to specify a different image transform for just the 2nd and fourth images in this grid.
Does the image ID relate to the order or to the specific image? How could I include a conditional in my code to output the largeIndexHalf image transform for the 2nd and 4th image?
{% set asset = entry.portraits %}
{% if asset | length %}
{% for asset in entry.portraits %}

{% endfor %}
{% endif %}

**Tags:** assets, image-transforms


---

## 2 answers

### Answer by username_2 on 2015-03-12. Score: 6 | Accepted answer

You could do that like so:
{% for asset in entry.portraits %}
    {% if loop.index == 2 or loop.index == 4 %}
        {% set transform = 'largeIndexHalf' %}
    {% else %}
        {% set transform = 'largeIndex' %}
    {% endif %}
    
{% endfor %}

or more simply (making use of the containment operator (in) and the ternary operator (? :):
{% for asset in entry.portraits %}
    {% set transform = loop.index in [2, 4] ? 'largeIndexHalf' : 'largeIndex' %}
    
{% endfor %}

> **Comments:**
> **username_0**: Works a treat. Thank you. Example here: http://alisilversteinportraits.com/  

---

### Answer by username_1 on 2015-03-12. Score: 3

To better understand, do you want transform for all even iterations on the array? If so, Twig's cycle function should do what you want:
{% set asset = entry.portraits %}
{% if asset | length %}
   {% for asset in entry.portraits %}
      {% set transform = cycle(['largeIndex', 'largeIndexHalf'], loop.index0) %}
      
   {% endfor %}
{% endif %}

Edit
Rereading the question and Brandon's answer, I may have misinterpreted that you do indeed just want the 2nd and 4th image. I'll leave my answer for posterity for people looking to do an even / odd thing though.


---

