Jinja 2 safe keyword

coredump picture coredump · Sep 9, 2012 · Viewed 23.7k times · Source

I have a little problem understanding what an expression like {{ something.render() | safe }} does .

From what I have seen, without the safe keyword it outputs the entire html document, not just the true content.

What I would like to know, is what it actually does, how it functions .

Answer

dkamins picture dkamins · Sep 9, 2012

Normally text is HTML-escaped (so <b> would be written out as &lt;b&gt;, which would render as <b>).

When you put |safe after something, you're telling the template engine that you have already escaped the text yourself, i.e. "it's safe to render this directly". So it will not do that encoding for you.

For more information: http://jinja.pocoo.org/docs/templates/#html-escaping