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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
46 deletions
+54
-46
pystache/template.py
+54
-46
No files found.
pystache/template.py
View file @
088b5713
...
...
@@ -4,40 +4,49 @@ 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."""
def
set
(
self
,
key
):
"""
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
setter
{{P yo }} => :P yo
"""
def
set_modifier
(
func
):
modifiers
[
symbol
]
=
func
return
func
return
set_modifier
class
Template
(
object
):
tag_re
=
None
otag
=
'{{'
ctag
=
'}}'
modifiers
=
Modifiers
()
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
from
view
import
View
self
.
template
=
template
if
kwargs
:
context
.
update
(
kwargs
)
self
.
view
=
context
if
isinstance
(
context
,
View
)
else
View
(
context
=
context
)
self
.
_compile_regexps
()
def
_compile_regexps
(
self
):
tags
=
{
'otag'
:
re
.
escape
(
self
.
otag
),
...
...
@@ -49,7 +58,7 @@ class Template(object):
tag
=
r"
%(otag)
s(#|=|&|!|>|\{)?(.+?)\1?
%(ctag)
s+"
self
.
tag_re
=
re
.
compile
(
tag
%
tags
)
def
_render_sections
(
self
,
template
,
view
):
while
True
:
match
=
self
.
section_re
.
search
(
template
)
...
...
@@ -79,23 +88,23 @@ class Template(object):
# Falsey and Negated or Truthy and Not Negated
elif
(
not
it
and
section
[
2
]
==
'^'
)
or
(
it
and
section
[
2
]
!=
'^'
):
replacer
=
inner
template
=
template
.
replace
(
section
,
replacer
)
return
template
def
_render_tags
(
self
,
template
):
while
True
:
match
=
self
.
tag_re
.
search
(
template
)
if
match
is
None
:
break
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
)
return
template
def
_render_dictionary
(
self
,
template
,
context
):
...
...
@@ -104,53 +113,53 @@ class Template(object):
out
=
template
.
render
()
self
.
view
.
context_list
.
pop
(
0
)
return
out
def
_render_list
(
self
,
template
,
listing
):
insides
=
[]
for
item
in
listing
:
insides
.
append
(
self
.
_render_dictionary
(
template
,
item
))
return
''
.
join
(
insides
)
@modifier
(
None
)
@modifier
s.set
(
None
)
def
_render_tag
(
self
,
tag_name
):
raw
=
self
.
view
.
get
(
tag_name
,
''
)
# For methods with no return value
if
not
raw
and
raw
is
not
0
:
return
''
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
,
''
))
def
render
(
self
,
encoding
=
None
):
template
=
self
.
_render_sections
(
self
.
template
,
self
.
view
)
result
=
self
.
_render_tags
(
template
)
if
encoding
is
not
None
:
result
=
result
.
encode
(
encoding
)
return
result
\ No newline at end of file
return
result
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