Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions chevron/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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':
Expand Down
14 changes: 14 additions & 0 deletions test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down