chutsu
chutsu

Reputation: 14103

How do I convert an array to string using the jinja template engine?

I have an array element called "tags" and would like to convert the array of tags to strings separated by a blank space. But how do you do that in Jinja?

I have tried:

{{ tags|join }}

Upvotes: 33

Views: 63177

Answers (2)

number5
number5

Reputation: 16453

Actually you are almost there, for join with space, just put it like this:

{{ tags|join(' ') }}

see the jinja docs for more details

Upvotes: 63

SingleNegationElimination
SingleNegationElimination

Reputation: 156248

You can use regular python in jinja tags. an 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 a z '

Upvotes: 6

Related Questions