Is it possible to html encoding the output in AppEngine templates?

So I am passing in an object with a content property containing the html.

<div>{{ myobject.content }}</div>

      

I want to be able to output content so that characters are displayed as HTML characters.

The "conent" content can be: <p> Hello </p>

I want this to be sent to the browser as: & ampl; p & ampgt; Hello & amplt; / p & gt;

Is there something I can add to my template to do this automatically?

0


a source to share


2 answers


Yes, it {{ myobject.content | escape }}

should help (assuming you mean Django templates - there is no specific App Engine templating system, GAE apps often use the Django templating system); you may need to repeat the part | escape

if you want two levels of escaping (as is the case in some, but not all of the examples you supply).



+4


a source


This is the Django function django.utils.html.escape:

def escape(html):
    """Returns the given HTML with ampersands, quotes and carets encoded."""
    return mark_safe(force_unicode(html).replace('&', '&amp;').replace('<', '&l
t;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;'))

      



Also see here .

+2


a source







All Articles