Alex Rivera | Logout

How do you mark strings as "Safe" in a view (or the template) in Jinja2?

Asked 2009-08-13T02:10:25.300
16

Typically when you want to mark string output as safe in Jinja2 you do something like this:

{{ output_string|safe() }}

However, what if output_string is always safe? I don't want to repeat myself every time by using the safe filter.

I have a custom filter called "emailize" that preps urls for output in an email. The ampersands always seem to become escaped. Is there a way in my custom filter to mark the output as safe?

Edit
Report

1 Answer

10

Use the Markup class:

class jinja2.Markup([string])

Marks a string as being safe for inclusion in HTML/XML output without needing to be escaped.

answered 2009-08-13T04:05:55.760

Your Answer