<p>I tried this and this worked out also:</p>

<pre><code>{% if loop.index is even %}
&lt;div&gt;&lt;/div&gt;
{% endif %}

{% if loop.index is odd %}
&lt;div&gt;&lt;/div&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2017-06-08T20:47:00.290Z
2017-06-08T20:47:00.290Z
Montier Kess
<p>Based on the <a href="https://twig.sensiolabs.org/doc/2.x/functions/cycle.html" rel="nofollow noreferrer">docs</a>:</p>

<pre><code>{% for event in events %}
    {{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}
</code></pre>

<p>or (achieves the same thing):</p>

<pre><code>{% for event in events %}
    {% set oddOrEven = cycle(['odd', 'even'], loop.index0) %}
    {{ oddOrEven }}
{% endfor %}
</code></pre>

<p>So, for different HTML:</p>

<pre><code>{% for event in events %}
    {% set oddOrEven = cycle(['odd', 'even'], loop.index0) %}
    {% if oddOrEven == 'odd' %}
        &lt;div class="Odd"&gt;Odd&lt;/div&gt;
    {% else %}
        &lt;div class="Even"&gt;Even&lt;/div&gt;
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2017-06-08T20:45:19.323Z
No worries. Does it answer your question?
Clive Portman
CC BY-SA 3.0
2017-06-08T20:32:05.593Z
2017-06-08T20:38:42.710Z
2017-06-08T20:38:42.710Z
Clive Portman
<p>Here is my code so far:</p>

<pre><code>{% for event in post.get_field('timeline_event') %}
{% set items = [odd, even]%}
{{ cycle (items, loop.index0) }}

{% if cycle == odd %}
&lt;div&gt;&lt;/div&gt;
{% endif %}

{% if cycle == even %}
&lt;div&gt;&lt;/div&gt;
{% endif %}
</code></pre>

<p>This is working for the most part, the issue is the code I have is being duplicated twice.</p>

<p>Here is an image: <a href="https://i.sstatic.net/8UQgU.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/8UQgU.jpg" alt="twig-cycle issue"></a></p>

<p>I know how to do this in jekyll liquid using capture, hit a roadblock using twig for php</p>

<templating><php>
Twig Cycle - how to show different html based on cycle
# Twig Cycle - how to show different html based on cycle

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

Here is my code so far:
{% for event in post.get_field('timeline_event') %}
{% set items = [odd, even]%}
{{ cycle (items, loop.index0) }}

{% if cycle == odd %}

{% endif %}

{% if cycle == even %}

{% endif %}

This is working for the most part, the issue is the code I have is being duplicated twice.
Here is an image: 
I know how to do this in jekyll liquid using capture, hit a roadblock using twig for php

**Tags:** templating, php


---

## 2 answers

### Answer by username_0 on 2017-06-08. Score: 4

I tried this and this worked out also:
{% if loop.index is even %}

{% endif %}

{% if loop.index is odd %}

{% endif %}


---

### Answer by username_1 on 2017-06-08. Score: 3

Based on the docs:
{% for event in events %}
    {{ cycle(['odd', 'even'], loop.index0) }}
{% endfor %}

or (achieves the same thing):
{% for event in events %}
    {% set oddOrEven = cycle(['odd', 'even'], loop.index0) %}
    {{ oddOrEven }}
{% endfor %}

So, for different HTML:
{% for event in events %}
    {% set oddOrEven = cycle(['odd', 'even'], loop.index0) %}
    {% if oddOrEven == 'odd' %}
        Odd
    {% else %}
        Even
    {% endif %}
{% endfor %}

> **Comments:**
> **username_1**: No worries. Does it answer your question?  

---

