Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-08T20:47:00.290Z
Answers.LastActivityDate: 2017-06-08T20:47:00.290Z
Answers.UserDisplayName: Montier Kess
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-08T20:45:19.323Z
Answers.Text: No worries. Does it answer your question?
Answers.UserDisplayName: Clive Portman
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-08T20:32:05.593Z
Answers.LastActivityDate: 2017-06-08T20:38:42.710Z
Answers.LastEditDate: 2017-06-08T20:38:42.710Z
Answers.UserDisplayName: Clive Portman
Body: <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>
  
Tags: <templating><php>
Title: Twig Cycle - how to show different html based on cycle
ThreadText: # 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 %}
  <div></div>
  {% endif %}
  
  {% if cycle == even %}
  <div></div>
  {% endif %}
  
  ```
  
  This is working for the most part, the issue is the code I have is being duplicated twice.
  
  Here is an image: [![twig-cycle issue](https://i.sstatic.net/8UQgU.jpg)](https://i.sstatic.net/8UQgU.jpg)
  
  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 %}
  <div></div>
  {% endif %}
  
  {% if loop.index is odd %}
  <div></div>
  {% endif %}
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2017-06-08. Score: 3
  
  Based on the [docs](https://twig.sensiolabs.org/doc/2.x/functions/cycle.html):
  
  ```
  {% 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' %}
          <div class="Odd">Odd</div>
      {% else %}
          <div class="Even">Even</div>
      {% endif %}
  {% endfor %}
  
  ```
  
  > **Comments:**
  > **username_1**: No worries. Does it answer your question?  
  
  ---
  
  