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
a28dfaef
Commit
a28dfaef
authored
Oct 29, 2009
by
Chris Wanstrath
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move Template into its own file
parent
1bd85306
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
62 deletions
+62
-62
pystache/__init__.py
+1
-62
pystache/template.py
+61
-0
No files found.
pystache/__init__.py
View file @
a28dfaef
import
r
e
from
pystache.template
import
Templat
e
class
Pystache
(
object
):
@staticmethod
def
render
(
template
,
context
=
{}):
return
Template
(
template
,
context
)
.
render
()
class
Template
(
object
):
tag_types
=
{
None
:
'tag'
,
'!'
:
'comment'
}
def
__init__
(
self
,
template
,
context
=
{}):
self
.
template
=
template
self
.
context
=
context
def
render
(
self
,
template
=
None
,
context
=
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
)
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?}}+"
)
match
=
re
.
search
(
regexp
,
template
)
while
match
:
tag
,
tag_type
,
tag_name
=
match
.
group
(
0
,
1
,
2
)
func
=
'render_'
+
self
.
tag_types
[
tag_type
]
if
hasattr
(
self
,
func
):
replacement
=
getattr
(
self
,
func
)(
tag_name
,
context
)
template
=
template
.
replace
(
tag
,
replacement
)
match
=
re
.
search
(
regexp
,
template
)
return
template
def
render_tag
(
self
,
tag_name
,
context
):
"""Given a tag name and context, finds and renders the tag."""
if
tag_name
in
context
:
return
context
[
tag_name
]
else
:
return
''
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
"""Rendering a comment always returns nothing."""
return
''
pystache/template.py
0 → 100644
View file @
a28dfaef
import
re
class
Template
(
object
):
tag_types
=
{
None
:
'tag'
,
'!'
:
'comment'
}
def
__init__
(
self
,
template
,
context
=
{}):
self
.
template
=
template
self
.
context
=
context
def
render
(
self
,
template
=
None
,
context
=
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
)
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?}}+"
)
match
=
re
.
search
(
regexp
,
template
)
while
match
:
tag
,
tag_type
,
tag_name
=
match
.
group
(
0
,
1
,
2
)
func
=
'render_'
+
self
.
tag_types
[
tag_type
]
if
hasattr
(
self
,
func
):
replacement
=
getattr
(
self
,
func
)(
tag_name
,
context
)
template
=
template
.
replace
(
tag
,
replacement
)
match
=
re
.
search
(
regexp
,
template
)
return
template
def
render_tag
(
self
,
tag_name
,
context
):
"""Given a tag name and context, finds and renders the tag."""
if
tag_name
in
context
:
return
context
[
tag_name
]
else
:
return
''
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
"""Rendering a comment always returns nothing."""
return
''
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