Created
January 17, 2014 08:45
-
-
Save wiedi/8470251 to your computer and use it in GitHub Desktop.
json2html python template tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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