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')
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] }} .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With