Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig, add first and last class

Tags:

php

twig

symfony

Let's say I have a for loop like this:

{% for elem in arrMenu %}     <div class="topmenu-button">         <a href="{{ elem.url }}">{{ elem.name }}</a>     </div> {% endfor %} 

In that form, it would render something like:

<div class="topmenu-button"><a href="url">name</a></div> <div class="topmenu-button"><a href="url">name</a></div> <div class="topmenu-button"><a href="url">name</a></div> <div class="topmenu-button"><a href="url">name</a></div> 

How can twig help me to add first and last classes the the div, so that I would have a result like:

<div class="topmenu-button first"><a href="url">name</a></div> <div class="topmenu-button"><a href="url">name</a></div> <div class="topmenu-button"><a href="url">name</a></div> <div class="topmenu-button last"><a href="url">name</a></div> 
like image 202
FMaz008 Avatar asked Oct 20 '11 14:10

FMaz008


People also ask

How do you add classes in twig?

To add multiple classes to an element, create an array with all of the class names. To create an array in Twig, use the set tag followed by the name of the array. And then pass that array to the attributes. addClass() function.

What is Loop index0?

loop.index0. The current iteration of the loop. ( 0 indexed) loop.revindex. The number of iterations from the end of the loop (1 indexed)


2 Answers

You can use the "special variables" loop.first and loop.last for what you want.

LINK

{% for elem in arrMenu %}     {% if loop.first %}     <div class="topmenu-button first">             {% elseif loop.last %}     <div class="topmenu-button last">             {% else %}     <div class="topmenu-button">             {% endif %}         <a href="{{ elem.url }}">{{ elem.name }}</a>     </div> {% endfor %} 

EDIT: depending how much you care about the "one case", you might need a solution like this.

{% for elem in arrMenu %}     {% set classes = ["topmenu-button"] %}     {% if loop.first %}{% set classes = classes|merge(["first"]) %}{% endif %}     {% if loop.last %}{% set classes = classes|merge(["last"]) %}{% endif %}     <div class="{{ classes|join(" ") }}">                 <a href="{{ elem.url }}">{{ elem.name }}</a>     </div> {% endfor %} 
like image 102
Kendall Hopkins Avatar answered Oct 05 '22 09:10

Kendall Hopkins


Since a loop can't be first and last at the same time, i would prefer not to use elseif and write (DRY - what if you have to change topmenu-button in future?):

{% for elem in arrMenu %}     {% set append %}         {% if loop.first %}first{% endif %}         {% if loop.last %}last{% endif %}     {% endset %}     <div class="topmenu-button {{ append }}">         <a href="{{ elem.url }}">{{ elem.name }}</a>     </div> {% endfor %} 

or more concise:

{% for elem in arrMenu %}     <div class="topmenu-button {% if loop.first %}first{% endif %} {% if loop.last %}last{% endif %}">         <a href="{{ elem.url }}">{{ elem.name }}</a>     </div> {% endfor %} 

Edit: this way you'll get some extra spaces in class attribute (it's perfectly fine btw). Edit2: this will not handle 1-element array (first = last)

like image 43
gremo Avatar answered Oct 05 '22 09:10

gremo