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
1bd85306
Commit
1bd85306
authored
Oct 29, 2009
by
Chris Wanstrath
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
basic true / false sections
parent
b51bd2e5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
4 deletions
+42
-4
pystache/__init__.py
+16
-0
tests/test_pystache.py
+26
-4
No files found.
pystache/__init__.py
View file @
1bd85306
...
...
@@ -19,8 +19,24 @@ class Template(object):
"""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
)
def
render_sections
(
self
,
template
,
context
):
regexp
=
re
.
compile
(
r"{{\#([^\}]*)}}\s*(.+?){{/\1}}"
)
match
=
re
.
search
(
regexp
,
template
)
while
match
:
section
,
section_name
,
inner
=
match
.
group
(
0
,
1
,
2
)
if
section_name
in
context
and
context
[
section_name
]:
return
template
.
replace
(
section
,
inner
)
else
:
return
template
.
replace
(
section
,
''
)
match
=
re
.
search
(
regexp
,
template
)
return
template
def
render_tags
(
self
,
template
,
context
):
"""Renders all the tags in a template for a context."""
regexp
=
re
.
compile
(
r"{{(#|=|!|<|>|\{)?(.+?)\1?}}+"
)
...
...
tests/test_pystache.py
View file @
1bd85306
...
...
@@ -21,7 +21,29 @@ class TestPystache(unittest.TestCase):
ret
=
Pystache
.
render
(
template
)
self
.
assertEquals
(
ret
,
"What what?"
)
def
xtest_sections
(
self
):
template
=
"I think {{name}} wants a {{thing}}, right {{name}}?"
ret
=
Pystache
.
render
(
template
,
{
'name'
:
'Jon'
,
'thing'
:
'racecar'
})
self
.
assertEquals
(
ret
,
"I think Jon wants a racecar, right Jon?"
)
def
test_false_sections_are_hidden
(
self
):
template
=
"Ready {{#set}}set {{/set}}go!"
ret
=
Pystache
.
render
(
template
,
{
'set'
:
False
})
self
.
assertEquals
(
ret
,
"Ready go!"
)
def
test_true_sections_are_shown
(
self
):
template
=
"Ready {{#set}}set{{/set}} go!"
ret
=
Pystache
.
render
(
template
,
{
'set'
:
True
})
self
.
assertEquals
(
ret
,
"Ready set go!"
)
def
aaxtest_sections
(
self
):
template
=
"""
<ul>
{{#users}}
<li>{{name}}</li>
{{/users}}
</ul>
"""
context
=
{
'users'
:
[
{
'name'
:
'Chris'
},
{
'name'
:
'Tom'
},
{
'name'
:
'PJ'
}
]
}
ret
=
Pystache
.
render
(
template
,
context
)
self
.
assertEquals
(
ret
,
"""<ul>
<li>Chris</li>
<li>Tom</li>
<li>PJ</li>
</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