Search code examples
stringjinja2

String concatenation in Jinja


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%}

Solution

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

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

    See join filter documentation, as well as filters in general documentation.

    p.s.

    More reader friendly way

    {{ my ~ ', ' ~ string }}