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
29ac86c0
Commit
29ac86c0
authored
Apr 29, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'issue-114-lambda-list-in-section' into 'development'
parents
54eb4b50
13d24c44
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
16 deletions
+53
-16
HISTORY.rst
+3
-1
pystache/renderengine.py
+34
-15
pystache/tests/test_renderengine.py
+16
-0
No files found.
HISTORY.rst
View file @
29ac86c0
...
...
@@ -5,7 +5,9 @@ History
-----------
* Bugfix: falsey values now coerced to strings using str().
* Bugfix: section-lambda return values no longer pushed onto context stack.
* Bugfix: issue #113: lambda return values for sections no longer pushed
onto context stack.
* Bugfix: issue #114: lists of lambdas for sections were not rendered.
0.5.1 (2012-04-24)
------------------
...
...
pystache/renderengine.py
View file @
29ac86c0
...
...
@@ -137,6 +137,7 @@ class RenderEngine(object):
# TODO: is there a bug because we are not using the same
# logic as in _get_string_value()?
data
=
context
.
get
(
name
)
# Per the spec, lambdas in inverted sections are considered truthy.
if
data
:
return
u''
return
parsed_template
.
render
(
context
)
...
...
@@ -155,21 +156,19 @@ class RenderEngine(object):
template
=
template_
parsed_template
=
parsed_template_
data
=
context
.
get
(
name
)
# From the spec:
#
# If the data is not of a list type, it is coerced into a list
# as follows: if the data is truthy (e.g. `!!data == true`),
# use a single-element list containing the data, otherwise use
# an empty list.
#
if
not
data
:
data
=
[]
elif
callable
(
data
):
# TODO: should we check the arity?
template
=
data
(
template
)
parsed_template
=
self
.
_parse
(
template
,
delimiters
=
delims
)
# Lambdas special case section rendering and bypass pushing
# the data value onto the context stack. Also see--
#
# https://github.com/defunkt/pystache/issues/113
#
return
parsed_template
.
render
(
context
)
else
:
# The
cleanest, least brittle way of determining whether
# s
omething s
upports iteration is by trying to call iter() on it:
# The
least brittle way to determine whether something
# supports iteration is by trying to call iter() on it:
#
# http://docs.python.org/library/functions.html#iter
#
...
...
@@ -183,14 +182,34 @@ class RenderEngine(object):
# Then the value does not support iteration.
data
=
[
data
]
else
:
# We treat the value as a list (but do not treat strings
# and dicts as lists).
if
isinstance
(
data
,
(
basestring
,
dict
)):
# Do not treat strings and dicts (which are iterable) as lists.
data
=
[
data
]
# Otherwise,
leave it alone
.
# Otherwise,
treat the value as a list
.
parts
=
[]
for
element
in
data
:
if
callable
(
element
):
# Lambdas special case section rendering and bypass pushing
# the data value onto the context stack. From the spec--
#
# When used as the data value for a Section tag, the
# lambda MUST be treatable as an arity 1 function, and
# invoked as such (passing a String containing the
# unprocessed section contents). The returned value
# MUST be rendered against the current delimiters, then
# interpolated in place of the section.
#
# Also see--
#
# https://github.com/defunkt/pystache/issues/113
#
# TODO: should we check the arity?
new_template
=
element
(
template
)
parsed_template
=
self
.
_parse
(
new_template
,
delimiters
=
delims
)
parts
.
append
(
parsed_template
.
render
(
context
))
continue
context
.
push
(
element
)
parts
.
append
(
parsed_template
.
render
(
context
))
context
.
pop
()
...
...
pystache/tests/test_renderengine.py
View file @
29ac86c0
...
...
@@ -501,6 +501,22 @@ class RenderTests(unittest.TestCase, AssertStringMixin):
context
=
{
'person'
:
'Mom'
,
'test'
:
(
lambda
text
:
text
+
" :)"
)}
self
.
_assert_render
(
u'Hi Mom :)'
,
template
,
context
)
def
test_section__lambda__list
(
self
):
"""
Check that lists of lambdas are processed correctly for sections.
This test case is equivalent to a test submitted to the Mustache spec here:
https://github.com/mustache/spec/pull/47 .
"""
template
=
'<{{#lambdas}}foo{{/lambdas}}>'
context
=
{
'foo'
:
'bar'
,
'lambdas'
:
[
lambda
text
:
"~{{
%
s}}~"
%
text
,
lambda
text
:
"#{{
%
s}}#"
%
text
]}
self
.
_assert_render
(
u'<~bar~#bar#>'
,
template
,
context
)
def
test_section__lambda__not_on_context_stack
(
self
):
"""
Check that section lambdas are not pushed onto the context stack.
...
...
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