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
088b5713
Commit
088b5713
authored
Feb 11, 2011
by
Zachary Voase
Committed by
Carl Whittaker
Feb 11, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Made the modifiers registry a class attribute rather than a global.
parent
01cfebe1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
17 deletions
+26
-17
pystache/template.py
+26
-17
No files found.
pystache/template.py
View file @
088b5713
...
...
@@ -4,20 +4,27 @@ import collections
import
os
import
copy
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
class
Modifiers
(
dict
):
"""Dictionary with a decorator for assigning functions to keys."""
{{P yo }} => :P yo
def
set
(
self
,
key
):
"""
def
set_modifier
(
func
):
modifiers
[
symbol
]
=
func
Decorator function to set the given key to the decorated function.
>>> modifiers = {}
>>> @modifiers.set('P')
... def render_tongue(self, tag_name=None, context=None):
... return ":P
%
s"
%
tag_name
>>> modifiers
{'P': <function render_tongue at 0x...>}
"""
def
setter
(
func
):
self
[
key
]
=
func
return
func
return
set_modifier
return
setter
class
Template
(
object
):
...
...
@@ -27,6 +34,8 @@ class Template(object):
ctag
=
'}}'
modifiers
=
Modifiers
()
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
from
view
import
View
...
...
@@ -92,7 +101,7 @@ class Template(object):
tag
,
tag_type
,
tag_name
=
match
.
group
(
0
,
1
,
2
)
tag_name
=
tag_name
.
strip
()
func
=
modifiers
[
tag_type
]
func
=
self
.
modifiers
[
tag_type
]
replacement
=
func
(
self
,
tag_name
)
template
=
template
.
replace
(
tag
,
replacement
)
...
...
@@ -112,7 +121,7 @@ class Template(object):
return
''
.
join
(
insides
)
@modifier
(
None
)
@modifier
s.set
(
None
)
def
_render_tag
(
self
,
tag_name
):
raw
=
self
.
view
.
get
(
tag_name
,
''
)
...
...
@@ -122,26 +131,26 @@ class Template(object):
return
cgi
.
escape
(
unicode
(
raw
))
@modifier
(
'!'
)
@modifier
s.set
(
'!'
)
def
_render_comment
(
self
,
tag_name
):
return
''
@modifier
(
'>'
)
@modifier
s.set
(
'>'
)
def
_render_partial
(
self
,
template_name
):
from
pystache
import
Loader
markup
=
Loader
()
.
load_template
(
template_name
,
self
.
view
.
template_path
,
encoding
=
self
.
view
.
template_encoding
)
template
=
Template
(
markup
,
self
.
view
)
return
template
.
render
()
@modifier
(
'='
)
@modifier
s.set
(
'='
)
def
_change_delimiter
(
self
,
tag_name
):
"""Changes the Mustache delimiter."""
self
.
otag
,
self
.
ctag
=
tag_name
.
split
(
' '
)
self
.
_compile_regexps
()
return
''
@modifier
(
'{'
)
@modifier
(
'&'
)
@modifier
s.set
(
'{'
)
@modifier
s.set
(
'&'
)
def
render_unescaped
(
self
,
tag_name
):
"""Render a tag without escaping it."""
return
unicode
(
self
.
view
.
get
(
tag_name
,
''
))
...
...
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