Commit 117e3530 by Chris Jerdonek

Merge branch 'issue_51' into development: closing issue #51

parents a484f30f abeb2b28
...@@ -3,8 +3,9 @@ History ...@@ -3,8 +3,9 @@ History
Next Release (version TBD) Next Release (version TBD)
-------------------------- --------------------------
* Bugfix: Passing **kwargs to Template.__init__() with no context * Bugfix: Passing **kwargs to Template() modified the context. [cjerdonek]
raised an exception. [cjerdonek] * Bugfix: Passing **kwargs to Template() with no context raised an
exception. [cjerdonek]
* Bugfix: Whitespace surrounding sections is no longer altered, in * Bugfix: Whitespace surrounding sections is no longer altered, in
accordance with the mustache spec. [heliodor] accordance with the mustache spec. [heliodor]
* A custom template loader can now be passed to a View. [cjerdonek] * A custom template loader can now be passed to a View. [cjerdonek]
......
...@@ -18,7 +18,4 @@ def render(template, context=None, **kwargs): ...@@ -18,7 +18,4 @@ def render(template, context=None, **kwargs):
Return the given template string rendered using the given context. Return the given template string rendered using the given context.
""" """
context = context and context.copy() or {} return Template(template, context, **kwargs).render()
context.update(kwargs)
return Template(template, context).render()
...@@ -62,6 +62,11 @@ class Template(object): ...@@ -62,6 +62,11 @@ class Template(object):
modifiers = Modifiers() modifiers = Modifiers()
def __init__(self, template=None, context=None, **kwargs): def __init__(self, template=None, context=None, **kwargs):
"""
The **kwargs arguments are only supported if the context is
a dictionary (i.e. not a View).
"""
from .view import View from .view import View
self.template = template self.template = template
...@@ -69,8 +74,11 @@ class Template(object): ...@@ -69,8 +74,11 @@ class Template(object):
if context is None: if context is None:
context = {} context = {}
if kwargs: if not isinstance(context, View):
context.update(kwargs) # Views do not support copy() or update().
context = context.copy()
if kwargs:
context.update(kwargs)
self.view = context if isinstance(context, View) else View(context=context) self.view = context if isinstance(context, View) else View(context=context)
self._compile_regexps() self._compile_regexps()
......
...@@ -12,6 +12,8 @@ from pystache.template import Template ...@@ -12,6 +12,8 @@ from pystache.template import Template
class TemplateTestCase(unittest.TestCase): class TemplateTestCase(unittest.TestCase):
"""Test the Template class."""
def test_init__kwargs_with_no_context(self): def test_init__kwargs_with_no_context(self):
""" """
Test passing **kwargs with no context. Test passing **kwargs with no context.
...@@ -20,3 +22,12 @@ class TemplateTestCase(unittest.TestCase): ...@@ -20,3 +22,12 @@ class TemplateTestCase(unittest.TestCase):
# This test checks that the following line raises no exception. # This test checks that the following line raises no exception.
template = Template(foo="bar") template = Template(foo="bar")
def test_init__kwargs_does_not_modify_context(self):
"""
Test that passing **kwargs does not modify the passed context.
"""
context = {}
template = Template(context=context, foo="bar")
self.assertEquals(context, {})
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