Commit abfba610 by Eric Naeseth Committed by Chris Wanstrath

Adding template file encoding awareness.

parent db1b35cc
<p>If alive today, Henri Poincaré would be {{age}} years old.</p>
\ No newline at end of file
import pystache
class UnicodeInput(pystache.View):
template_path = 'examples'
template_encoding = 'utf8'
def age(self):
return 156
...@@ -19,6 +19,10 @@ class View(object): ...@@ -19,6 +19,10 @@ class View(object):
# Contents of the template. # Contents of the template.
template = None template = None
# Character encoding of the template file. If None, Pystache will not
# do any decoding of the template.
template_encoding = None
def __init__(self, template=None, context=None, **kwargs): def __init__(self, template=None, context=None, **kwargs):
self.template = template self.template = template
...@@ -57,6 +61,8 @@ class View(object): ...@@ -57,6 +61,8 @@ class View(object):
f = open(self.template_file, 'r') f = open(self.template_file, 'r')
try: try:
template = f.read() template = f.read()
if self.template_encoding:
template = unicode(template, self.template_encoding)
finally: finally:
f.close() f.close()
return template return template
......
...@@ -10,6 +10,7 @@ from examples.unescaped import Unescaped ...@@ -10,6 +10,7 @@ from examples.unescaped import Unescaped
from examples.template_partial import TemplatePartial from examples.template_partial import TemplatePartial
from examples.delimiters import Delimiters from examples.delimiters import Delimiters
from examples.unicode_output import UnicodeOutput from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput
class TestView(unittest.TestCase): class TestView(unittest.TestCase):
def test_comments(self): def test_comments(self):
...@@ -27,6 +28,10 @@ class TestView(unittest.TestCase): ...@@ -27,6 +28,10 @@ class TestView(unittest.TestCase):
def test_encoded_output(self): def test_encoded_output(self):
self.assertEquals(UnicodeOutput().render('utf8'), '<p>Name: Henri Poincar\xc3\xa9</p>') self.assertEquals(UnicodeOutput().render('utf8'), '<p>Name: Henri Poincar\xc3\xa9</p>')
def test_unicode_input(self):
self.assertEquals(UnicodeInput().render(),
u'<p>If alive today, Henri Poincaré would be 156 years old.</p>')
def test_escaped(self): def test_escaped(self):
self.assertEquals(Escaped().render(), "<h1>Bear &gt; Shark</h1>") self.assertEquals(Escaped().render(), "<h1>Bear &gt; Shark</h1>")
......
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