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
fe276bfe
Commit
fe276bfe
authored
Nov 12, 2009
by
Chris Wanstrath
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add fancy @modifier decorator
parent
f7689a92
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
14 deletions
+23
-14
pystache/template.py
+23
-14
No files found.
pystache/template.py
View file @
fe276bfe
import
re
import
re
import
cgi
import
cgi
modifiers
=
{}
def
modifier
(
symbol
):
"""Decorator for associating a function with a Mustache tag modifier.
@modifier('P')
def render_tongue(self, tag_name=None, context=None):
return ":P
%
s"
%
tag_name
{{P yo }} => :P yo
"""
def
set_modifier
(
func
):
modifiers
[
symbol
]
=
func
return
func
return
set_modifier
class
Template
(
object
):
class
Template
(
object
):
# The regular expression used to find a #section
# The regular expression used to find a #section
section_re
=
None
section_re
=
None
...
@@ -14,15 +29,6 @@ class Template(object):
...
@@ -14,15 +29,6 @@ class Template(object):
# Closing tag delimiter
# Closing tag delimiter
ctag
=
'}}'
ctag
=
'}}'
# Names of different tag modifiers, used to render them.
tag_types
=
{
None
:
'tag'
,
'!'
:
'comment'
,
'{'
:
'unescaped'
,
'>'
:
'partial'
,
'='
:
'delimiter'
}
def
__init__
(
self
,
template
,
context
=
None
):
def
__init__
(
self
,
template
,
context
=
None
):
self
.
template
=
template
self
.
template
=
template
self
.
context
=
context
or
{}
self
.
context
=
context
or
{}
...
@@ -79,26 +85,28 @@ class Template(object):
...
@@ -79,26 +85,28 @@ class Template(object):
tag
,
tag_type
,
tag_name
=
match
.
group
(
0
,
1
,
2
)
tag
,
tag_type
,
tag_name
=
match
.
group
(
0
,
1
,
2
)
tag_name
=
tag_name
.
strip
()
tag_name
=
tag_name
.
strip
()
func
=
'render_'
+
self
.
tag_types
[
tag_type
]
func
=
modifiers
[
tag_type
]
replacement
=
func
(
self
,
tag_name
,
context
)
if
hasattr
(
self
,
func
):
template
=
template
.
replace
(
tag
,
replacement
)
replacement
=
getattr
(
self
,
func
)(
tag_name
,
context
)
template
=
template
.
replace
(
tag
,
replacement
)
return
template
return
template
@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
(
context
.
get
(
tag_name
,
''
))
return
cgi
.
escape
(
context
.
get
(
tag_name
,
''
))
@modifier
(
'!'
)
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
"""Rendering a comment always returns nothing."""
"""Rendering a comment always returns nothing."""
return
''
return
''
@modifier
(
'{'
)
def
render_unescaped
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_unescaped
(
self
,
tag_name
=
None
,
context
=
None
):
"""Render a tag without escaping it."""
"""Render a tag without escaping it."""
return
context
.
get
(
tag_name
,
''
)
return
context
.
get
(
tag_name
,
''
)
@modifier
(
'>'
)
def
render_partial
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_partial
(
self
,
tag_name
=
None
,
context
=
None
):
"""Renders a partial within the current context."""
"""Renders a partial within the current context."""
# Import view here to avoid import loop
# Import view here to avoid import loop
...
@@ -109,6 +117,7 @@ class Template(object):
...
@@ -109,6 +117,7 @@ class Template(object):
return
view
.
render
()
return
view
.
render
()
@modifier
(
'='
)
def
render_delimiter
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_delimiter
(
self
,
tag_name
=
None
,
context
=
None
):
"""Changes the Mustache delimiter."""
"""Changes the Mustache delimiter."""
self
.
otag
,
self
.
ctag
=
tag_name
.
split
(
' '
)
self
.
otag
,
self
.
ctag
=
tag_name
.
split
(
' '
)
...
...
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