olbanana
olbanana

Reputation: 87

Symfony2: What's the equivalent to PHP's explode() function in twig?

I am fetching some results from database through doctrine.

The resulting array for e.g. ads.attraction has strings (words) with commas inside.

How can I separate each word from the comma and display each of them on it's own line?

Upvotes: 3

Views: 3906

Answers (1)

Nicolai Fröhlich
Nicolai Fröhlich

Reputation: 52493

solution:

The twig split filter converts a string to an array by given delimiter.

Afterwards you can output each element of this array using a for loop.

example:

{% set array = "one,two,three"|split(',') %} 

{% for element in array %}
    {{ element }}<br /> 
{% endfor %}

Just replace "one,two,three" with your variable (ads.attraction) in your concrete case.

Upvotes: 12

Related Questions