Skip to main content

Working in Laravel, I needed to loop through an array and know whether the current loop was odd or even row.  To do this add the expression to your Twig file:

{% set direction = loop.index0 is odd ? 'left' : 'right' %}

In the call above, the variable direction is set to either left or right depending on the value of the position of the loop row (loop.index0).  I then used the left or right value in a div class adding to a series of tailwind css definitions.  Such as text-{{ direction }}

The full div tag in this instance was:

<div class="font-light block text-5xl py-5 px-10 text-{{ direction }} tracking-wider text-white">{{ item.title }}</div>

The expression odd returns true if the given number is odd:

{{ var is odd }}

Therefore in the set direction call above, when odd is true the value of direction will be left.

 

Other twig set calls
{% set foo = 'bar' %}

After the set call, the foo variable is available in the template like any other ones:

{# displays bar #}

{{ foo }}

The assigned value can be any valid Twig expression:

{% set foo = [1, 2] %}

{% set foo = {'foo': 'bar'} %}

{% set foo = 'foo' ~ 'bar' %}

Several variables can be assigned in one block:

{% set foo, bar = 'foo', 'bar' %}

The above call is equivalent to writing:

{% set foo = 'foo' %}

{% set bar = 'bar' %}

The set tag can also be used to ‘capture’ chunks of text:

{% set foo %}
    <div id="pagination">
        ...
    </div>
{% endset %}

If you enable automatic output escaping, Twig will only consider the content to be safe when capturing chunks of text.

Note, that loops are scoped in Twig; therefore a variable declared inside a for loop is not accessible outside the loop itself:

{% for item in list %}
    {% set foo = item %}
{% endfor %}

{# foo is NOT available #}

If you want to access the variable, just declare it before the loop:

{% set foo = "" %}
{% for item in list %}
    {% set foo = item %}
{% endfor %}

{# foo is available #}

Related articles

Andrew Fletcher18 Dec 2023
Using Twig {% extends %}
In Twig, the {% extends %} tag is used to inherit and extend the contents of another template. In the case you provided:{% extends "filed--text.html.twig" %}This means that the current template is extending the content of the template file named "filed--text.html.twig." The contents of the extended...
Andrew Fletcher08 Nov 2023
Using paragraph.getParentEntity() in Drupal Twig templates
In Drupal Twig templates, you can set a variable like paragraph_parent to the parent entity of a paragraph using the paragraph.getParentEntity() method. This is a common approach when you want to access the parent entity (e.g., a node) that contains the paragraph. &nbsp;Here's how you can do it:{#...
Andrew Fletcher14 Oct 2023
Twig\Error\SyntaxError: Unexpected "spaceless" tag
Update Drupal to 10.1.x and I'm receiving the following errorTwig\Error\SyntaxError: Unexpected "spaceless" tag (expecting closing tag for the "macro" tag defined near line 2). in Twig\Parser-&gt;subparse() (line 2 of themes/custom/{theme}/templates/misc/filesize-macro.html.twig).The error message...