How to convert an array to a string using jinja's template engine?

I have an array element called tags and would like to convert the tag array to a string separated by a space. But how do you do it in Jing?

I tried:

{{ tags|join }} 
+8
python flask jinja2
source share
2 answers

Actually, you are almost there, to connect to the space, just enter it like this:

 {{ tags|join(' ') }} 

see jinja docs for more details

+18
source share

You can use regular python in jinja tags. The obvious choice for some simple cases is str.join :

 >>> jinja2.Template(r'{{ " ".join(bar) }}').render(bar='baz') u'b a z'. 

You can also iterate over sequences in jinja with a for block:

 >>> jinja2.Template(r'{% for quux in bar %}{{ quux }} {% endfor %}').render(bar='baz') u'b az ' 
+2
source share

All Articles