Implement tojson jinja filter for those using Jinja < 2.9

This commit is contained in:
Erik Johnson 2018-06-25 20:43:34 -05:00
parent 5b8d55428e
commit 1499c6abcf
No known key found for this signature in database
GPG key ID: 5E5583C437808F3F

View file

@ -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):
'''