Skip to content

Instantly share code, notes, and snippets.

@wiedi
Created January 17, 2014 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wiedi/8470251 to your computer and use it in GitHub Desktop.
Save wiedi/8470251 to your computer and use it in GitHub Desktop.
json2html python template tag
from django import template
from django.utils.safestring import mark_safe
from django.utils.html import conditional_escape
register = template.Library()
def json2html(val):
out = ''
if type(val) == list:
out += '<ul>'
for i in val:
out += '<li>' + json2html(i) + '</li>'
out += '</ul>'
return mark_safe(out)
elif type(val) == dict:
out += '<ul>'
for prop in val.keys():
out += '<li><strong>' + prop + ':</strong> ' + json2html(val[prop]) + '</li>'
out += '</ul>'
return mark_safe(out)
else:
return conditional_escape(unicode(val))
register.filter('json2html', json2html)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment