Commit 4ce9bf1d by Chris Jerdonek

Merge branch 'issue_44' into development: closing issue #44

parents ab1d2fb1 fc4882cf
...@@ -3,6 +3,7 @@ History ...@@ -3,6 +3,7 @@ History
Next Release (version TBD) Next Release (version TBD)
-------------------------- --------------------------
* Bugfix: context values no longer processed as template strings. [jakearchibald]
* API change: pass the context to render to Template.render() instead of * API change: pass the context to render to Template.render() instead of
Template.__init__(). [cjerdonek] Template.__init__(). [cjerdonek]
* Bugfix: Passing **kwargs to Template() modified the context. [cjerdonek] * Bugfix: Passing **kwargs to Template() modified the context. [cjerdonek]
......
...@@ -169,18 +169,28 @@ class Template(object): ...@@ -169,18 +169,28 @@ class Template(object):
return template return template
def _render_tags(self, template): def _render_tags(self, template):
output = []
while True: while True:
match = self.tag_re.search(template) parts = self.tag_re.split(template, maxsplit=1)
if match is None: output.append(parts[0])
if len(parts) < 2:
# Then there was no match.
break break
tag, tag_type, tag_name = match.group(0, 1, 2) start, tag_type, tag_name, template = parts
tag_name = tag_name.strip() tag_name = tag_name.strip()
func = self.modifiers[tag_type] func = self.modifiers[tag_type]
replacement = func(self, tag_name) tag_value = func(self, tag_name)
template = template.replace(tag, replacement)
return template # Appending the tag value to the output prevents treating the
# value as a template string (bug: issue #44).
output.append(tag_value)
output = "".join(output)
return output
def _render_dictionary(self, template, context): def _render_dictionary(self, template, context):
self.context.push(context) self.context.push(context)
......
...@@ -82,3 +82,22 @@ class TemplateTestCase(unittest.TestCase): ...@@ -82,3 +82,22 @@ class TemplateTestCase(unittest.TestCase):
self.assertTrue(isinstance(actual, str)) self.assertTrue(isinstance(actual, str))
self.assertEquals(actual, 'Poincaré') self.assertEquals(actual, 'Poincaré')
def test_render__tag_in_value(self):
"""
Context values should not be treated as templates (issue #44).
"""
template = Template('{{test}}')
context = {'test': '{{hello}}'}
actual = template.render(context)
self.assertEquals(actual, '{{hello}}')
def test_render__section_in_value(self):
"""
Context values should not be treated as templates (issue #44).
"""
template = Template('{{test}}')
context = {'test': '{{#hello}}'}
actual = template.render(context)
self.assertEquals(actual, '{{#hello}}')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment