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%}
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.
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 }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With