diff --git a/chevron/renderer.py b/chevron/renderer.py index 65a00f6..0cac700 100644 --- a/chevron/renderer.py +++ b/chevron/renderer.py @@ -130,7 +130,7 @@ def _get_partial(name, partials_dict, partials_path, partials_ext): def render(template='', data={}, partials_path='.', partials_ext='mustache', partials_dict={}, padding='', def_ldel='{{', def_rdel='}}', - scopes=None, warn=False, keep=False): + scopes=None, warn=False, keep=False, escape=_html_escape): """Render a mustache template. Renders a mustache template with a data scope and partial capability. @@ -179,6 +179,8 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', keep -- Keep unreplaced tags when a template substitution isn't found in the data + escape -- Escape entries before replacement (defaults to html escaping) + Returns: @@ -238,7 +240,7 @@ def render(template='', data={}, partials_path='.', partials_ext='mustache', thing = scopes[1] if not isinstance(thing, unicode_type): thing = unicode(str(thing), 'utf-8') - output += _html_escape(thing) + output += escape(thing) # If we're a no html escape tag elif tag == 'no escape': diff --git a/test_spec.py b/test_spec.py index 905e105..dd31b7e 100755 --- a/test_spec.py +++ b/test_spec.py @@ -552,6 +552,20 @@ def test_keep_from_partials(self): expected = '1st {{ missing_key }} 3rd' self.assertEqual(result, expected) + def test_escape(self): + args = { + 'template': '{{ testing }}', + 'data': { + 'testing': '"123&;', + }, + 'escape': lambda s: s.replace('"', "foo").replace("123", "bar").replace("&;", "!"), + } + + result = chevron.render(**args) + expected = 'foobar!' + + self.assertEqual(result, expected) + # Run unit tests from command line if __name__ == "__main__":