certainlyakey
11/19/2017 - 8:14 PM

Easy way to alternate something (classes for example) every X and X times in Twig

Easy way to alternate something (classes for example) every X and X times in Twig

{# Example: 2/6/2/6/2/6... #}
{# first batch + second batch sum #}
{% for batch in posts|batch(8) %}
      
  {% for item in batch %}

    {% set size = 'class-A' %}
    
    {# first batch + 1 #}
    {% if loop.index < 3 %}
      {% set size = 'class-B' %}
    {% endif %}
    
    {% include 'partials/teaser-partial.twig' with {'post':item, 'size':size} %}
  {% endfor %}

{% endfor %}