Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove the first item of an array in twig?

Tags:

I have a array called 'Posts' in twig.

Is there a way to easily remove the first item of this array?

So it's structure is just like this:

array('post 1','post 2','post 3')

And I was wondering if the first post can easily be removed with a function in twig to this:

array('post 2','post 3')
like image 489
Noob17 Avatar asked Nov 16 '15 19:11

Noob17


People also ask

How do you access an array in twig?

Accessing array elements Twig as a parameter can receive array. To access a specific element of array you can use regular php array access bracket notation {{ array[key] }} .


1 Answers

You're looking for the slice filter.

The slice filter extracts a slice of a sequence, a mapping, or a string:

{% for i in ['post 1', 'post 2', 'post 3'] | slice(1) %} 
    {{ i }}
{% endfor %}

output

post 2      
post 3 

The slice filter works as the array_slice PHP function for arrays and mb_substr for strings with a fallback to substr.

like image 81
Simone Nigro Avatar answered Sep 18 '22 13:09

Simone Nigro