Skip to content

Commit

Permalink
Adding support for Unicode and non-ASCII-encoded bytestring output.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Naeseth committed Jan 16, 2010
1 parent e38a953 commit f123343
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions examples/unicode_output.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Name: {{name}}</p>
9 changes: 9 additions & 0 deletions examples/unicode_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# encoding: utf-8

import pystache

class UnicodeOutput(pystache.View):
template_path = 'examples'

def name(self):
return u'Henri Poincaré'
9 changes: 6 additions & 3 deletions pystache/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ def __init__(self, template, context=None):
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."""
Expand Down Expand Up @@ -94,7 +97,7 @@ def render_tags(self, template, context):
@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):
Expand Down
4 changes: 2 additions & 2 deletions pystache/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def get(self, attr, default):
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()
9 changes: 9 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

import unittest
import pystache

Expand All @@ -7,6 +9,7 @@
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):
Expand All @@ -18,6 +21,12 @@ def test_double_section(self):
* 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>")

Expand Down
8 changes: 8 additions & 0 deletions tests/test_pystache.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# encoding: utf-8

import unittest
import pystache

Expand Down Expand Up @@ -49,6 +51,12 @@ def test_non_strings(self):
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>
Expand Down

0 comments on commit f123343

Please sign in to comment.