Commit 112ac76d by Eric Naeseth Committed by Chris Wanstrath

Adding support for Unicode and non-ASCII-encoded bytestring output.

parent e38a953b
<p>Name: {{name}}</p>
\ No newline at end of file
# encoding: utf-8
import pystache
class UnicodeOutput(pystache.View):
template_path = 'examples'
def name(self):
return u'Henri Poincaré'
......@@ -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):
......
......@@ -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()
# 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>")
......
# 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>
......
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