2

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

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

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' %}
        <div class="Odd">Odd</div>
    {% else %}
        <div class="Even">Even</div>
    {% endif %}
{% endfor %}
  • No worries. Does it answer your question? – Clive Portman Jun 8 '17 at 20:45
3

I tried this and this worked out also:

{% if loop.index is even %}
<div></div>
{% endif %}

{% if loop.index is odd %}
<div></div>
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.