Comma separated list in twig

This works with Symfony 2.3.x but should work with every 2.x version:

{{ user.roles|join(', ') }}

Don't know about shortest but this could be clear. Try the following to add comma after all lines in the loop except the last one:

{% for role in user.roles %}
    {{ role.name }}
    {% if not loop.last %},{% endif %}
{% endfor %}

Shorter version as suggested in comments:

{% for role in user.roles %}
    {{ role.name }}
    {{ not loop.last ? ',' }}
{% endfor %}

Tags:

Php

Twig