stacey.mosier
stacey.mosier

Reputation: 413

Using a counter in a Twig node template

Setting up a Drupal 8 custom view for Top 4 items, I want a different layout for item 1 than the remaining. I have override files for the custom view, but for this example I'm using base files to keep it simple.

In views-view.html.twig base file we have:

<div class="view-content">
    {{ rows }}
</div>

In node.html.twig base file we have:

<div {{ content_attributes.addClass('content') }}>
    {{ content }}
</div>

In node.html.twig I am aiming for something like:

{% if row_counter = 1 %}
    output this markup / fields
{% else %}
   do something boring with the other 3 items.
{% endif %}

I was able to set row_counter in the views-view.twig.html file:

{% for row in rows %}
    {%  set row_counter = loop.index %}
    <div{{ row.attributes }}>
        {{ row_counter }}
    </div>
{% endfor %}

But I need to check against the value of {{ row_counter }} in the node.html.twig file....

What other properties are available in node.html.twig to check against its position in the list?

Upvotes: 2

Views: 5109

Answers (2)

Frank Drebin
Frank Drebin

Reputation: 1083

You could do something like this if a class is enough:

Your views template:

{% for row in rows %}
    <div class="{% if loop.index == 1 %}first_element{% endif %}">
      {{ row.content }}
    </div>
{% endfor %}

And then just style it appropriately.

Upvotes: 0

DarkBee
DarkBee

Reputation: 15622

From the documentation

The loop variable

Inside of a for loop block you can access some special variables:
Variable        Description
-----------------------------------------------------------------
loop.index      The current iteration of the loop. (1 indexed)
loop.index0     The current iteration of the loop. (0 indexed)
loop.revindex   The number of iterations from the end of the loop (1 indexed)
loop.revindex0  The number of iterations from the end of the loop (0 indexed)
loop.first      True if first iteration
loop.last       True if last iteration
loop.length     The number of items in the sequence
loop.parent     The parent context

edit: every variable know in the parent template is also known inside an include as context is passed by default. Only macro's don't know the parent's context

controller

<?php
    require __DIR__ . '/../requires/propel_standalone.php';

    echo $twig->render('tests/items.html', [
        'items' =>  [
                        'Abc',
                        'def',
                        'ghi',
                        'jkl',
                        'mno',
                        'pqr',
                        'stu',
                        'vwx',
                        'z',
                    ],
    ]);

items.twig

<!doctype>
<html>
    <head><title>Test</title></head>
    <body>
        {% for item in items %}
            {% include "tests/item.html" %}
        {% endfor %}
    </body>
</html>

item.twig

{% set order = 'Nothing to report' %}
{% if loop.first %}
    {% set order = 'I\'m first' %}
{% endif %}
{% if loop.last %}
    {% set order = 'I\'m last' %}
{% endif %}

{% if loop.index is even %}
    {% set order = 'I\'m even' %}
{% endif %}

{% if loop.index is divisible by(5) %}
    {% set order = 'I can be dived by 5' %}
{% endif %}

{% if loop.index is divisible by(3) %}
    {% set order = 'I can be dived by 3' %}
{% endif %}


<div>
    <b>{{ loop.index }}:</b>{{ order }} - {{ item }}
</div>

Upvotes: 4

Related Questions