Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pystache_custom
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
pystache_custom
Commits
112ac76d
Commit
112ac76d
authored
Jan 17, 2010
by
Eric Naeseth
Committed by
Chris Wanstrath
Feb 09, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding support for Unicode and non-ASCII-encoded bytestring output.
parent
e38a953b
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
36 additions
and
5 deletions
+36
-5
examples/unicode_output.mustache
+2
-0
examples/unicode_output.py
+9
-0
pystache/template.py
+6
-3
pystache/view.py
+2
-2
tests/test_examples.py
+9
-0
tests/test_pystache.py
+8
-0
No files found.
examples/unicode_output.mustache
0 → 100644
View file @
112ac76d
<p>
Name:
{{
name
}}
</p>
\ No newline at end of file
examples/unicode_output.py
0 → 100644
View file @
112ac76d
# encoding: utf-8
import
pystache
class
UnicodeOutput
(
pystache
.
View
):
template_path
=
'examples'
def
name
(
self
):
return
u'Henri Poincaré'
pystache/template.py
View file @
112ac76d
...
@@ -34,13 +34,16 @@ class Template(object):
...
@@ -34,13 +34,16 @@ class Template(object):
self
.
context
=
context
or
{}
self
.
context
=
context
or
{}
self
.
compile_regexps
()
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."""
"""Turns a Mustache template into something wonderful."""
template
=
template
or
self
.
template
template
=
template
or
self
.
template
context
=
context
or
self
.
context
context
=
context
or
self
.
context
template
=
self
.
render_sections
(
template
,
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
):
def
compile_regexps
(
self
):
"""Compiles our section and tag regular expressions."""
"""Compiles our section and tag regular expressions."""
...
@@ -94,7 +97,7 @@ class Template(object):
...
@@ -94,7 +97,7 @@ class Template(object):
@modifier
(
None
)
@modifier
(
None
)
def
render_tag
(
self
,
tag_name
,
context
):
def
render_tag
(
self
,
tag_name
,
context
):
"""Given a tag name and context, finds, escapes, and renders the tag."""
"""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
(
'!'
)
@modifier
(
'!'
)
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
...
...
pystache/view.py
View file @
112ac76d
...
@@ -83,9 +83,9 @@ class View(object):
...
@@ -83,9 +83,9 @@ class View(object):
else
:
else
:
return
attr
return
attr
def
render
(
self
):
def
render
(
self
,
encoding
=
None
):
template
=
self
.
load_template
()
template
=
self
.
load_template
()
return
Template
(
template
,
self
)
.
render
()
return
Template
(
template
,
self
)
.
render
(
encoding
=
encoding
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
render
()
return
self
.
render
()
tests/test_examples.py
View file @
112ac76d
# encoding: utf-8
import
unittest
import
unittest
import
pystache
import
pystache
...
@@ -7,6 +9,7 @@ from examples.escaped import Escaped
...
@@ -7,6 +9,7 @@ from examples.escaped import Escaped
from
examples.unescaped
import
Unescaped
from
examples.unescaped
import
Unescaped
from
examples.template_partial
import
TemplatePartial
from
examples.template_partial
import
TemplatePartial
from
examples.delimiters
import
Delimiters
from
examples.delimiters
import
Delimiters
from
examples.unicode_output
import
UnicodeOutput
class
TestView
(
unittest
.
TestCase
):
class
TestView
(
unittest
.
TestCase
):
def
test_comments
(
self
):
def
test_comments
(
self
):
...
@@ -18,6 +21,12 @@ class TestView(unittest.TestCase):
...
@@ -18,6 +21,12 @@ class TestView(unittest.TestCase):
* second
* second
* third"""
)
* 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
):
def
test_escaped
(
self
):
self
.
assertEquals
(
Escaped
()
.
render
(),
"<h1>Bear > Shark</h1>"
)
self
.
assertEquals
(
Escaped
()
.
render
(),
"<h1>Bear > Shark</h1>"
)
...
...
tests/test_pystache.py
View file @
112ac76d
# encoding: utf-8
import
unittest
import
unittest
import
pystache
import
pystache
...
@@ -49,6 +51,12 @@ class TestPystache(unittest.TestCase):
...
@@ -49,6 +51,12 @@ class TestPystache(unittest.TestCase):
ret
=
pystache
.
render
(
template
,
{
'stats'
:
stats
})
ret
=
pystache
.
render
(
template
,
{
'stats'
:
stats
})
self
.
assertEquals
(
ret
,
"""(123 & ['something'])(chris & 0.9)"""
)
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
):
def
test_sections
(
self
):
template
=
"""
template
=
"""
<ul>
<ul>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment