Tutorial :Is is possible to html encode output in AppEngine templates?



Question:

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?


Solution:1

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).


Solution: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.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »