Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Jinja Template '<br>'.join

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 %}
like image 750
Brian Carpio Avatar asked Feb 26 '13 19:02

Brian Carpio


1 Answers

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) }}
like image 177
mgibsonbr Avatar answered Oct 16 '22 23:10

mgibsonbr