Commit 5302934d by Chris Jerdonek

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

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