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
d5cee636
Commit
d5cee636
authored
Dec 21, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes issue #61: "Renderer class should handle load_template not returning unicode"
parent
cb7383e7
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
16 deletions
+27
-16
pystache/renderengine.py
+8
-4
pystache/renderer.py
+6
-1
tests/test_renderengine.py
+6
-7
tests/test_renderer.py
+7
-4
No files found.
pystache/renderengine.py
View file @
d5cee636
...
...
@@ -55,10 +55,14 @@ class RenderEngine(object):
modifiers
=
Modifiers
()
def
__init__
(
self
,
load_
template
=
None
,
literal
=
None
,
escape
=
None
):
def
__init__
(
self
,
load_
partial
=
None
,
literal
=
None
,
escape
=
None
):
"""
Arguments:
load_partial: a function for loading templates by name when
loading partials. The function should accept a template name
and return a unicode template string.
escape: a function that takes a unicode or str string,
converts it to unicode, and escapes and returns it.
...
...
@@ -68,7 +72,7 @@ class RenderEngine(object):
"""
self
.
escape
=
escape
self
.
literal
=
literal
self
.
load_
template
=
load_template
self
.
load_
partial
=
load_partial
def
render
(
self
,
template
,
context
):
"""
...
...
@@ -178,7 +182,7 @@ class RenderEngine(object):
@modifiers.set
(
'>'
)
def
_render_partial
(
self
,
template_name
):
markup
=
self
.
load_
template
(
template_name
)
markup
=
self
.
load_
partial
(
template_name
)
return
self
.
_render
(
markup
)
@modifiers.set
(
'='
)
...
...
@@ -194,7 +198,7 @@ class RenderEngine(object):
@modifiers.set
(
'{'
)
@modifiers.set
(
'&'
)
def
render_unescaped
(
self
,
tag_name
):
def
_
render_unescaped
(
self
,
tag_name
):
"""
Render a tag without escaping it.
...
...
pystache/renderer.py
View file @
d5cee636
...
...
@@ -138,7 +138,12 @@ class Renderer(object):
Return a RenderEngine instance for rendering.
"""
engine
=
RenderEngine
(
load_template
=
self
.
load_template
,
# Make sure the return value of load_template is unicode.
def
load_partial
(
name
):
template
=
self
.
load_template
(
name
)
return
self
.
unicode
(
template
)
engine
=
RenderEngine
(
load_partial
=
load_partial
,
literal
=
self
.
literal
,
escape
=
self
.
_unicode_and_escape
)
return
engine
...
...
tests/test_renderengine.py
View file @
d5cee636
...
...
@@ -21,13 +21,12 @@ class RenderEngineTestCase(unittest.TestCase):
Create and return a default RenderEngine for testing.
"""
load_template
=
None
to_unicode
=
unicode
escape
=
lambda
s
:
cgi
.
escape
(
to_unicode
(
s
))
literal
=
to_unicode
engine
=
RenderEngine
(
literal
=
literal
,
escape
=
escape
,
load_
template
=
None
)
engine
=
RenderEngine
(
literal
=
literal
,
escape
=
escape
,
load_
partial
=
None
)
return
engine
def
_assert_render
(
self
,
expected
,
template
,
*
context
,
**
kwargs
):
...
...
@@ -35,7 +34,7 @@ class RenderEngineTestCase(unittest.TestCase):
engine
=
kwargs
.
get
(
'engine'
,
self
.
_engine
())
if
partials
is
not
None
:
engine
.
load_
template
=
lambda
key
:
partials
[
key
]
engine
.
load_
partial
=
lambda
key
:
partials
[
key
]
context
=
Context
(
*
context
)
...
...
@@ -49,23 +48,23 @@ class RenderEngineTestCase(unittest.TestCase):
"""
# In real-life, these arguments would be functions
engine
=
RenderEngine
(
load_
template
=
"load_template
"
,
literal
=
"literal"
,
escape
=
"escape"
)
engine
=
RenderEngine
(
load_
partial
=
"foo
"
,
literal
=
"literal"
,
escape
=
"escape"
)
self
.
assertEquals
(
engine
.
load_partial
,
"foo"
)
self
.
assertEquals
(
engine
.
escape
,
"escape"
)
self
.
assertEquals
(
engine
.
literal
,
"literal"
)
self
.
assertEquals
(
engine
.
load_template
,
"load_template"
)
def
test_render
(
self
):
self
.
_assert_render
(
'Hi Mom'
,
'Hi {{person}}'
,
{
'person'
:
'Mom'
})
def
test_render__load_
template
(
self
):
def
test_render__load_
partial
(
self
):
"""
Test that render() uses the load_template attribute.
"""
engine
=
self
.
_engine
()
partials
=
{
'partial'
:
"{{person}}"
}
engine
.
load_
template
=
lambda
key
:
partials
[
key
]
engine
.
load_
partial
=
lambda
key
:
partials
[
key
]
self
.
_assert_render
(
'Hi Mom'
,
'Hi {{>partial}}'
,
{
'person'
:
'Mom'
},
engine
=
engine
)
def
test_render__literal
(
self
):
...
...
tests/test_renderer.py
View file @
d5cee636
...
...
@@ -244,16 +244,19 @@ class RendererTestCase(unittest.TestCase):
# correctly, we no longer need to test the rendering code paths through
# the Renderer. We can test rendering paths through only the RenderEngine
# for the same amount of code coverage.
def
test_make_render_engine__load_
template
(
self
):
def
test_make_render_engine__load_
partial
(
self
):
"""
Test that _make_render_engine()
passes the right load_template
.
Test that _make_render_engine()
constructs and passes load_partial correctly
.
"""
renderer
=
Renderer
()
renderer
.
load_template
=
"foo"
# in real life, this would be a function.
renderer
.
unicode
=
lambda
s
:
s
.
upper
()
# a test version.
# In real-life, the partial would be different with each name.
renderer
.
load_template
=
lambda
name
:
"partial"
engine
=
renderer
.
_make_render_engine
()
self
.
assertEquals
(
engine
.
load_template
,
"foo"
)
# Make sure it calls unicode.
self
.
assertEquals
(
engine
.
load_partial
(
'name'
),
"PARTIAL"
)
def
test_make_render_engine__literal
(
self
):
"""
...
...
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