0

So, I'm passing an object with a "content" property that contains html.

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

I want to be able to output the content so that the characters are rendered as the html characters.

The contents of "conent" might be: <p>Hello</p>

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

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

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
Jim
  • 11,229
  • 20
  • 79
  • 114

2 Answers2

4

Yes, {{ myobject.content | escape }} should help (assuming you mean Django templates -- there's no specific "App Engine" templating system, GAE apps often use the Django templating system); you may need to repeat the | escape part if you want two levels of escaping (as appears to be the case in some but not all of the example you supply).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • My small app is using Google's webapp framework. But I think that framework does default to using the Django template system. – Jim May 20 '09 at 17:36
2

This is Django's django.utils.html.escape function:

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.

Community
  • 1
  • 1
T Banes
  • 115
  • 1
  • 7