2

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('-')}} 

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

My comment doesn't make any sense I mean't {% else %}-{% endfor %} indeed, just found it strange to use twig to just output a string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.