J. M.
J. M.

Reputation: 35

Twig - loop default value

I am trying to print array from the cotroller into the twig temlate. I want to print "-" whenever array is NULL. My problem is that in for-loop case it writes nothing, however single row working fine. Is there some simple way how to do it correctly?

this is not working as i expected

{% for key  in keywords|default('-') %} 
    {{ key~', '}} 
{% endfor %}

this is working

{{ key |default('-')}} 

Upvotes: 2

Views: 480

Answers (1)

Darkstarone
Darkstarone

Reputation: 4730

You can use an {% else %} construct on a for loop to do something else if the array is null:

{% for key  in keywords %} 
    {{ key~', '}} 
{% else %}
    -
{% endfor %}

See the documentation here.

Upvotes: 2

Related Questions