From abfba610b2072ea801ec27b7c44f69a18c00f768 Mon Sep 17 00:00:00 2001 From: Eric Naeseth Date: Sun, 17 Jan 2010 06:58:53 +0800 Subject: Adding template file encoding awareness. --- examples/unicode_input.mustache | 1 + examples/unicode_input.py | 8 ++++++++ pystache/view.py | 6 ++++++ tests/test_examples.py | 5 +++++ 4 files changed, 20 insertions(+) create mode 100644 examples/unicode_input.mustache create mode 100644 examples/unicode_input.py 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 @@ +

If alive today, Henri Poincaré would be {{age}} years old.

\ 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'), '

Name: Henri Poincar\xc3\xa9

') + def test_unicode_input(self): + self.assertEquals(UnicodeInput().render(), + u'

If alive today, Henri Poincaré would be 156 years old.

') + def test_escaped(self): self.assertEquals(Escaped().render(), "

Bear > Shark

") -- cgit v1.2.1