summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Naeseth <enaeseth@gmail.com>2010-01-17 06:35:50 +0800
committerChris Wanstrath <chris@ozmm.org>2010-02-09 18:35:26 +0800
commit112ac76d615bd7051daeaabc170298a854d4c0f1 (patch)
tree72340129f79043df1b05214c0813646b7879b9bd
parente38a953b8f3f835756a9c92aac2a073f7a76ef60 (diff)
downloadpystache-112ac76d615bd7051daeaabc170298a854d4c0f1.tar.gz
Adding support for Unicode and non-ASCII-encoded bytestring output.
-rw-r--r--examples/unicode_output.mustache1
-rw-r--r--examples/unicode_output.py9
-rw-r--r--pystache/template.py9
-rw-r--r--pystache/view.py4
-rw-r--r--tests/test_examples.py9
-rw-r--r--tests/test_pystache.py8
6 files changed, 35 insertions, 5 deletions
diff --git a/examples/unicode_output.mustache b/examples/unicode_output.mustache
new file mode 100644
index 0000000..8495f56
--- /dev/null
+++ b/examples/unicode_output.mustache
@@ -0,0 +1 @@
+<p>Name: {{name}}</p> \ No newline at end of file
diff --git a/examples/unicode_output.py b/examples/unicode_output.py
new file mode 100644
index 0000000..3cb9260
--- /dev/null
+++ b/examples/unicode_output.py
@@ -0,0 +1,9 @@
+# encoding: utf-8
+
+import pystache
+
+class UnicodeOutput(pystache.View):
+ template_path = 'examples'
+
+ def name(self):
+ return u'Henri Poincaré'
diff --git a/pystache/template.py b/pystache/template.py
index 5ab0d09..787304d 100644
--- a/pystache/template.py
+++ b/pystache/template.py
@@ -34,13 +34,16 @@ class Template(object):
self.context = context or {}
self.compile_regexps()
- def render(self, template=None, context=None):
+ def render(self, template=None, context=None, encoding=None):
"""Turns a Mustache template into something wonderful."""
template = template or self.template
context = context or self.context
template = self.render_sections(template, context)
- return self.render_tags(template, context)
+ result = self.render_tags(template, context)
+ if encoding is not None:
+ result = result.encode(encoding)
+ return result
def compile_regexps(self):
"""Compiles our section and tag regular expressions."""
@@ -94,7 +97,7 @@ class Template(object):
@modifier(None)
def render_tag(self, tag_name, context):
"""Given a tag name and context, finds, escapes, and renders the tag."""
- return cgi.escape(str(context.get(tag_name, '') or ''))
+ return cgi.escape(unicode(context.get(tag_name, '') or ''))
@modifier('!')
def render_comment(self, tag_name=None, context=None):
diff --git a/pystache/view.py b/pystache/view.py
index e8a4f7a..bbed285 100644
--- a/pystache/view.py
+++ b/pystache/view.py
@@ -83,9 +83,9 @@ class View(object):
else:
return attr
- def render(self):
+ def render(self, encoding=None):
template = self.load_template()
- return Template(template, self).render()
+ return Template(template, self).render(encoding=encoding)
def __str__(self):
return self.render()
diff --git a/tests/test_examples.py b/tests/test_examples.py
index 4f64fac..e073efb 100644
--- a/tests/test_examples.py
+++ b/tests/test_examples.py
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
import unittest
import pystache
@@ -7,6 +9,7 @@ from examples.escaped import Escaped
from examples.unescaped import Unescaped
from examples.template_partial import TemplatePartial
from examples.delimiters import Delimiters
+from examples.unicode_output import UnicodeOutput
class TestView(unittest.TestCase):
def test_comments(self):
@@ -18,6 +21,12 @@ class TestView(unittest.TestCase):
* second
* third""")
+ def test_unicode_output(self):
+ self.assertEquals(UnicodeOutput().render(), u'<p>Name: Henri Poincaré</p>')
+
+ def test_encoded_output(self):
+ self.assertEquals(UnicodeOutput().render('utf8'), '<p>Name: Henri Poincar\xc3\xa9</p>')
+
def test_escaped(self):
self.assertEquals(Escaped().render(), "<h1>Bear &gt; Shark</h1>")
diff --git a/tests/test_pystache.py b/tests/test_pystache.py
index c6f0122..4298180 100644
--- a/tests/test_pystache.py
+++ b/tests/test_pystache.py
@@ -1,3 +1,5 @@
+# encoding: utf-8
+
import unittest
import pystache
@@ -49,6 +51,12 @@ class TestPystache(unittest.TestCase):
ret = pystache.render(template, { 'stats': stats })
self.assertEquals(ret, """(123 & ['something'])(chris & 0.9)""")
+ def test_unicode(self):
+ template = 'Name: {{name}}; Age: {{age}}'
+ ret = pystache.render(template, { 'name': u'Henri Poincaré',
+ 'age': 156 })
+ self.assertEquals(ret, u'Name: Henri Poincaré; Age: 156')
+
def test_sections(self):
template = """
<ul>