I have a list:
list = ['var','var','var']
In my Jinja template I want to do:
{{'<br>'.join(list)}}
But the <br>
actually shows on the page. Is there a way to do this without adding another
{% for item in list %}
{{item}}
<br>
{% endfor %}
If every element in the list is safe (i.e. does not contain markup, or characters that should be escaped before being inserted in the result) then you can mark it as such:
{{'<br>'.join(list)|safe}}
To be sure, you should escape every item on list
before feeding it to the template engine, if you want to use it that way. Otherwise, your page may become vulnerable to HTML Injection/XSS (especially if your list contains user submitted data).
Update: as pointed out by @Doobeh, the join
filter accepts a custom safe separator, so you can use that instead, and the contents of list
will still be escaped:
{{ list|join('<br>'|safe) }}
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