Commit 5302934d by Chris Jerdonek

Moved the encoding argument from Template.render() to Template.__init__().

parent 90a0ffc5
...@@ -62,7 +62,7 @@ class Template(object): ...@@ -62,7 +62,7 @@ class Template(object):
modifiers = Modifiers() modifiers = Modifiers()
def __init__(self, template=None, context=None, load_template=None, **kwargs): def __init__(self, template=None, context=None, load_template=None, output_encoding=None, **kwargs):
""" """
Construct a Template instance. Construct a Template instance.
...@@ -77,6 +77,11 @@ class Template(object): ...@@ -77,6 +77,11 @@ class Template(object):
accept a single template_name parameter and return a template as accept a single template_name parameter and return a template as
a string. Defaults to the default Loader's load_template() method. a string. Defaults to the default Loader's load_template() method.
output_encoding: the encoding to use when rendering to a string.
The argument should be the name of an encoding as a string, for
example "utf-8". See the render() method's documentation for more
information.
""" """
if context is None: if context is None:
context = {} context = {}
...@@ -96,6 +101,7 @@ class Template(object): ...@@ -96,6 +101,7 @@ class Template(object):
self.context = context self.context = context
self.load_template = load_template self.load_template = load_template
self.output_encoding = output_encoding
self.template = template self.template = template
self._compile_regexps() self._compile_regexps()
...@@ -237,23 +243,19 @@ class Template(object): ...@@ -237,23 +243,19 @@ class Template(object):
""" """
return literal(self.context.get(tag_name, '')) return literal(self.context.get(tag_name, ''))
def render(self, encoding=None): def render(self):
""" """
Return the template rendered using the current context. Return the template rendered using the current context.
The return value is a unicode string, unless the encoding argument The return value is a unicode string, unless the output_encoding
is not None, in which case the return value has type str (encoded attribute is not None, in which case the return value has type str
using that encoding). and is encoded using that encoding.
Arguments:
encoding: the name of the encoding as a string, for example "utf-8".
""" """
template = self._render_sections(self.template) template = self._render_sections(self.template)
result = self._render_tags(template) result = self._render_tags(template)
if encoding is not None: if self.output_encoding is not None:
result = result.encode(encoding) result = result.encode(self.output_encoding)
return result return result
...@@ -93,8 +93,8 @@ class View(object): ...@@ -93,8 +93,8 @@ class View(object):
Return the view rendered using the current context. Return the view rendered using the current context.
""" """
template = Template(self.get_template(), self.context, self.load_template) template = Template(self.get_template(), self.context, self.load_template, output_encoding=encoding)
return template.render(encoding=encoding) return template.render()
def get(self, key, default=None): def get(self, key, default=None):
return self.context.get(key, default) return self.context.get(key, default)
......
...@@ -56,7 +56,8 @@ class TemplateTestCase(unittest.TestCase): ...@@ -56,7 +56,8 @@ class TemplateTestCase(unittest.TestCase):
def test_render__output_encoding(self): def test_render__output_encoding(self):
template = Template(u'Poincaré') template = Template(u'Poincaré')
actual = template.render('utf-8') template.output_encoding = 'utf-8'
actual = template.render()
self.assertTrue(isinstance(actual, str)) self.assertTrue(isinstance(actual, str))
self.assertEquals(actual, 'Poincaré') self.assertEquals(actual, 'Poincaré')
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