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
ca0ad881
Commit
ca0ad881
authored
Dec 31, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved more methods into Template class.
parent
1b04089e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
45 deletions
+52
-45
pystache/template.py
+52
-45
No files found.
pystache/template.py
View file @
ca0ad881
...
...
@@ -37,13 +37,6 @@ def call(val, view, template=None):
return
unicode
(
val
)
def
parse_to_tree
(
template
,
view
,
delims
=
(
'{{'
,
'}}'
)):
template
=
Template
(
template
)
template
.
view
=
view
template
.
otag
,
template
.
ctag
=
delims
template
.
_compile_regexps
()
return
template
.
parse_to_tree
()
def
render_parse_tree
(
parse_tree
,
view
,
template
):
"""
Convert a parse-tree into a string.
...
...
@@ -54,40 +47,6 @@ def render_parse_tree(parse_tree, view, template):
return
''
.
join
(
parts
)
def
render
(
template
,
view
,
delims
=
(
'{{'
,
'}}'
)):
"""
Arguments:
template: template string
view: context
"""
parse_tree
=
parse_to_tree
(
template
,
view
,
delims
)
return
render_parse_tree
(
parse_tree
,
view
,
template
)
def
sectionTag
(
name
,
parse_tree_
,
template_
,
delims
):
def
func
(
self
):
template
=
template_
parse_tree
=
parse_tree_
data
=
self
.
get
(
name
)
if
not
data
:
return
''
elif
callable
(
data
):
template
=
call
(
val
=
data
,
view
=
self
,
template
=
template
)
parse_tree
=
parse_to_tree
(
template
,
self
,
delims
)
data
=
[
data
]
elif
type
(
data
)
not
in
[
list
,
tuple
]:
data
=
[
data
]
parts
=
[]
for
element
in
data
:
self
.
context_list
.
insert
(
0
,
element
)
parts
.
append
(
render_parse_tree
(
parse_tree
,
self
,
delims
))
del
self
.
context_list
[
0
]
return
''
.
join
(
parts
)
return
func
def
inverseTag
(
name
,
parsed
,
template
,
delims
):
def
func
(
self
):
data
=
self
.
get
(
name
)
...
...
@@ -151,16 +110,53 @@ class Template(object):
def
func
(
context
):
val
=
context
.
get
(
name
)
template
=
call
(
val
=
val
,
view
=
context
)
return
self
.
to_unicode
(
render
(
template
,
context
))
return
self
.
to_unicode
(
self
.
render_template
(
template
,
context
))
return
func
def
partial_tag_function
(
self
,
name
,
indentation
=
''
):
def
func
(
context
):
nonblank
=
re
.
compile
(
r'^(.)'
,
re
.
M
)
template
=
re
.
sub
(
nonblank
,
indentation
+
r'\1'
,
self
.
partial
(
name
,
context
))
return
render
(
template
,
context
)
return
self
.
render_template
(
template
,
context
)
return
func
def
section_tag_function
(
self
,
name
,
parse_tree_
,
template_
,
delims
):
def
func
(
context
):
template
=
template_
parse_tree
=
parse_tree_
data
=
context
.
get
(
name
)
if
not
data
:
return
''
elif
callable
(
data
):
template
=
call
(
val
=
data
,
view
=
context
,
template
=
template
)
parse_tree
=
self
.
parse_string_to_tree
(
template
,
context
,
delims
)
data
=
[
data
]
elif
type
(
data
)
not
in
[
list
,
tuple
]:
data
=
[
data
]
parts
=
[]
for
element
in
data
:
context
.
context_list
.
insert
(
0
,
element
)
parts
.
append
(
render_parse_tree
(
parse_tree
,
context
,
delims
))
del
context
.
context_list
[
0
]
return
''
.
join
(
parts
)
return
func
def
parse_string_to_tree
(
self
,
template
,
view
,
delims
=
(
'{{'
,
'}}'
)):
template
=
Template
(
template
)
template
.
view
=
view
template
.
to_unicode
=
self
.
to_unicode
template
.
escape
=
self
.
escape
template
.
partial
=
self
.
partial
template
.
otag
,
template
.
ctag
=
delims
template
.
_compile_regexps
()
return
template
.
parse_to_tree
()
def
parse_to_tree
(
self
,
index
=
0
):
"""
Parse a template into a syntax tree.
...
...
@@ -235,7 +231,7 @@ class Template(object):
tmpl
=
e
.
template
end_index
=
e
.
position
tag
=
se
ctionTag
if
captures
[
'tag'
]
==
'#'
else
inverseTag
tag
=
se
lf
.
section_tag_function
if
captures
[
'tag'
]
==
'#'
else
inverseTag
func
=
tag
(
name
,
bufr
,
tmpl
,
(
self
.
otag
,
self
.
ctag
))
elif
captures
[
'tag'
]
in
[
'{'
,
'&'
]:
...
...
@@ -258,8 +254,19 @@ class Template(object):
return
end_index
def
render_template
(
self
,
template
,
view
,
delims
=
(
'{{'
,
'}}'
)):
"""
Arguments:
template: template string
view: context
"""
parse_tree
=
self
.
parse_string_to_tree
(
template
,
view
,
delims
)
return
render_parse_tree
(
parse_tree
,
view
,
template
)
def
render
(
self
,
encoding
=
None
):
result
=
render
(
self
.
template
,
self
.
view
)
result
=
self
.
render_template
(
self
.
template
,
self
.
view
)
if
encoding
is
not
None
:
result
=
result
.
encode
(
encoding
)
...
...
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