How to alternate loop content

Created on 9 August 2023, over 1 year ago
Updated 10 August 2023, over 1 year ago

Problem/Motivation

How do I alternate loop output content in Twig for paragraphs? Below are the 2 templates I use. Using Drupal 9. Thanks

paragraph--X.html.twig:

{% block paragraph %}
 {% block content %}
 {% set isEven = loop.index % 2 == 0 %}

     {% if isEven %}  //tried {% if loop is odd %} too
     DO X
    {% else %}
     DO Y
    {% endif %}
  
{% endblock %}
{% endblock paragraph %}

field--paragraph--X.html.twig:

Can I loop alternate content in the below somehow?

{% for item in items %}
    {{ item.content }} 
 {% endfor %}

Expected Output:

DO X
DO Y
DO X
DO Y
DO X
DO Y
DO X
DO Y
...

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

πŸ’¬ Support request
Status

Fixed

Version

1.15

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States hcanning

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024