diff options
author | Eric Naeseth <enaeseth@gmail.com> | 2010-01-17 06:58:53 +0800 |
---|---|---|
committer | Chris Wanstrath <chris@ozmm.org> | 2010-02-09 18:35:30 +0800 |
commit | abfba610b2072ea801ec27b7c44f69a18c00f768 (patch) | |
tree | 8e627211c8197727d29f8a3d82c30f5b75611aa2 | |
parent | db1b35ccb21f70f698c1c9387d533f01358f0f04 (diff) | |
download | pystache-abfba610b2072ea801ec27b7c44f69a18c00f768.tar.gz |
Adding template file encoding awareness.
-rw-r--r-- | examples/unicode_input.mustache | 1 | ||||
-rw-r--r-- | examples/unicode_input.py | 8 | ||||
-rw-r--r-- | pystache/view.py | 6 | ||||
-rw-r--r-- | tests/test_examples.py | 5 |
4 files changed, 20 insertions, 0 deletions
diff --git a/examples/unicode_input.mustache b/examples/unicode_input.mustache new file mode 100644 index 0000000..9b5e335 --- /dev/null +++ b/examples/unicode_input.mustache @@ -0,0 +1 @@ +<p>If alive today, Henri Poincaré would be {{age}} years old.</p>
\ No newline at end of file diff --git a/examples/unicode_input.py b/examples/unicode_input.py new file mode 100644 index 0000000..9f3684f --- /dev/null +++ b/examples/unicode_input.py @@ -0,0 +1,8 @@ +import pystache + +class UnicodeInput(pystache.View): + template_path = 'examples' + template_encoding = 'utf8' + + def age(self): + return 156 diff --git a/pystache/view.py b/pystache/view.py index 15c6942..81a6d33 100644 --- a/pystache/view.py +++ b/pystache/view.py @@ -19,6 +19,10 @@ class View(object): # Contents of the template. 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): self.template = template @@ -57,6 +61,8 @@ class View(object): f = open(self.template_file, 'r') try: template = f.read() + if self.template_encoding: + template = unicode(template, self.template_encoding) finally: f.close() return template diff --git a/tests/test_examples.py b/tests/test_examples.py index e073efb..57eb104 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -10,6 +10,7 @@ from examples.unescaped import Unescaped from examples.template_partial import TemplatePartial from examples.delimiters import Delimiters from examples.unicode_output import UnicodeOutput +from examples.unicode_input import UnicodeInput class TestView(unittest.TestCase): def test_comments(self): @@ -27,6 +28,10 @@ class TestView(unittest.TestCase): def test_encoded_output(self): 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): self.assertEquals(Escaped().render(), "<h1>Bear > Shark</h1>") |