From 1499c6abcf8076a2fab43c608cc46ed93c9e6635 Mon Sep 17 00:00:00 2001 From: Erik Johnson Date: Mon, 25 Jun 2018 20:43:34 -0500 Subject: [PATCH] Implement tojson jinja filter for those using Jinja < 2.9 --- salt/utils/jinja.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/salt/utils/jinja.py b/salt/utils/jinja.py index aed3a21b7a6d..d98ead50f386 100644 --- a/salt/utils/jinja.py +++ b/salt/utils/jinja.py @@ -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): '''