Skip to content

Commit

Permalink
Implement tojson jinja filter for those using Jinja < 2.9
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalmage committed Jun 27, 2018
1 parent 5b8d554 commit 1499c6a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions salt/utils/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,26 @@ def to_bool(val):
return False


@jinja_filter('tojson')
def tojson(val, indent=None):
'''
Implementation of tojson filter (only present in Jinja 2.9 and later). If
Jinja 2.9 or later is installed, then the upstream version of this filter
will be used.
'''
options = {'ensure_ascii': True}
if indent is not None:
options['indent'] = indent
return (
salt.utils.json.dumps(
val, **options
).replace('<', '\\u003c')
.replace('>', '\\u003e')
.replace('&', '\\u0026')
.replace("'", '\\u0027')
)


@jinja_filter('quote')
def quote(txt):
'''
Expand Down

0 comments on commit 1499c6a

Please sign in to comment.