Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String concatenation in Jinja

Tags:

string

jinja2

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%} 
like image 202
KacieHouser Avatar asked Jan 14 '10 00:01

KacieHouser


People also ask

How do you add two strings in Jinja?

You can use + if you know all the values are strings. Jinja also provides the ~ operator, which will ensure all values are converted to string first.


1 Answers

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 }} 
like image 151
mechanical_meat Avatar answered Oct 17 '22 21:10

mechanical_meat