String concatenation in Jinja

KacieHouser picture KacieHouser · Jan 14, 2010 · Viewed 134.7k times · Source

I just want to loop through an existing list and make a comma delimited string out of it.
Something like this: my_string = 'stuff, stuff, stuff, stuff'

I already know about loop.last, I just need to know how to make the third line in my code below WORK.

{% set my_string = '' %}
{% for stuff in stuffs %}
{% set my_string = my_string + stuff + ', '%}
{% endfor%}

Answer

mechanical_meat picture mechanical_meat · Jan 14, 2010

If stuffs is a list of strings, just this would work:

{{ stuffs|join(", ") }}

Link to join filter documentation, link to filters in general documentation.