Skip to content

Commit

Permalink
Add a template tag to format JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
U039b committed Sep 21, 2024
1 parent 463a6b5 commit 02866cb
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions colander/core/templatetags/colander_tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from base64 import urlsafe_b64encode

from django import template
Expand Down Expand Up @@ -61,6 +62,18 @@ def split(instance, delim):
return words


@register.filter(name="json_format")
def json_format(obj, indent=2):
if not obj or type(obj) is not dict:
return obj
formatted = obj.copy()
try:
formatted = json.dumps(formatted, indent=indent)
except:
pass
return formatted


@register.simple_tag(takes_context=True)
def active_link(context, view_url, *args, **kwargs):
"""
Expand Down

0 comments on commit 02866cb

Please sign in to comment.