I want to put a variable between each array value when I display it via Twig.
Here is my code :
{% for genre in genres %}
{% for namez in genre.name|split(', ') %}
<span>{{ namez }}</span>
{% endfor %}
{% endfor %}
Strcutre of Array genres :
array (size=2)
0 =>
array (size=4)
'id' => string '2' (length=1)
'name' => string 'Documentaire' (length=12)
'movie_id' => string '2' (length=1)
'genre_id' => string '2' (length=1)
1 =>
array (size=4)
'id' => string '3' (length=1)
'name' => string 'Animation' (length=9)
'movie_id' => string '2' (length=1)
'genre_id' => string '3' (length=1)
My results: Documentaries Action Drama
Instead of: Documentary, Action, Drama
And maybe even if it is possible to apply the sort() PHP function on the Twig / PHP split() function.
My Answer:
{{ namez }},
{% endfor %}_{% endfor %}_This code will loop through each genre in the genres array and then split the name of each genre by the comma and space. It will then display each word with a comma after it.
Rate this post
5 of 5 based on 7190 votesComments