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
b7ab439a
Commit
b7ab439a
authored
Jan 11, 2011
by
Carl Whittaker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dictionaries and lists are now handled in sections.
parent
8fb76bb2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
8 deletions
+57
-8
examples/template_partial.mustache
+2
-5
pystache/template.py
+42
-1
pystache/view.py
+13
-2
No files found.
examples/template_partial.mustache
View file @
b7ab439a
<h1>
{{
title
}}
</h1>
<h1>
{{
title
}}
</h1>
{{>
inner_partial
}}
{{>
inner_partial
}}
{{#
looping
}}
\ No newline at end of file
{{>
inner_partial
}}
{{/
looping
}}
\ No newline at end of file
pystache/template.py
View file @
b7ab439a
...
@@ -43,10 +43,34 @@ class Template(object):
...
@@ -43,10 +43,34 @@ class Template(object):
'ctag'
:
re
.
escape
(
self
.
ctag
)
'ctag'
:
re
.
escape
(
self
.
ctag
)
}
}
section
=
r"
%(otag)
s[\#|^]([^\}]*)
%(ctag)
s\s*(.+?\s*)
%(otag)
s/\1
%(ctag)
s"
self
.
section_re
=
re
.
compile
(
section
%
tags
,
re
.
M
|
re
.
S
)
tag
=
r"
%(otag)
s(#|=|&|!|>|\{)?(.+?)\1?
%(ctag)
s+"
tag
=
r"
%(otag)
s(#|=|&|!|>|\{)?(.+?)\1?
%(ctag)
s+"
self
.
tag_re
=
re
.
compile
(
tag
%
tags
)
self
.
tag_re
=
re
.
compile
(
tag
%
tags
)
def
_render_sections
(
self
,
template
,
view
):
def
_render_sections
(
self
,
template
,
view
):
while
True
:
match
=
self
.
section_re
.
search
(
template
)
if
match
is
None
:
break
section
,
section_name
,
inner
=
match
.
group
(
0
,
1
,
2
)
section_name
=
section_name
.
strip
()
it
=
view
.
get
(
section_name
,
None
)
replacer
=
''
# Dictionary
if
it
and
hasattr
(
it
,
'keys'
)
and
hasattr
(
it
,
'__getitem__'
):
if
section
[
2
]
!=
'^'
:
replacer
=
self
.
_render_dictionary
(
inner
,
it
)
elif
it
and
hasattr
(
it
,
'__iter__'
):
if
section
[
2
]
!=
'^'
:
replacer
=
self
.
_render_list
(
inner
,
it
)
template
=
template
.
replace
(
section
,
replacer
)
return
template
return
template
def
_render_tags
(
self
,
template
,
view
):
def
_render_tags
(
self
,
template
,
view
):
...
@@ -63,7 +87,24 @@ class Template(object):
...
@@ -63,7 +87,24 @@ class Template(object):
return
template
return
template
def
_render_dictionary
(
self
,
template
,
context
):
from
view
import
View
view
=
View
(
context
=
context
)
view
.
parent
=
self
.
view
return
Template
(
template
,
view
)
.
render
()
def
_render_list
(
self
,
template
,
listing
):
from
view
import
View
insides
=
[]
for
item
in
listing
:
view
=
View
(
context
=
item
)
view
.
parent
=
self
.
view
insides
.
append
(
Template
(
template
,
view
)
.
render
())
return
''
.
join
(
insides
)
@modifier
(
None
)
@modifier
(
None
)
def
_render_tag
(
self
,
tag_name
,
view
):
def
_render_tag
(
self
,
tag_name
,
view
):
raw
=
view
.
get
(
tag_name
,
''
)
raw
=
view
.
get
(
tag_name
,
''
)
...
...
pystache/view.py
View file @
b7ab439a
...
@@ -11,4 +11,15 @@ class View(object):
...
@@ -11,4 +11,15 @@ class View(object):
self
.
context
.
update
(
**
kwargs
)
self
.
context
.
update
(
**
kwargs
)
def
get
(
self
,
attr
,
default
=
None
):
def
get
(
self
,
attr
,
default
=
None
):
return
self
.
context
.
get
(
attr
,
getattr
(
self
,
attr
,
default
))
attr
=
self
.
context
.
get
(
attr
,
getattr
(
self
,
attr
,
self
.
_get_from_parent
(
attr
,
default
)))
\ No newline at end of file
if
hasattr
(
attr
,
'__call__'
)
and
type
(
attr
)
is
UnboundMethodType
:
return
attr
()
else
:
return
attr
def
_get_from_parent
(
self
,
attr
,
default
=
None
):
if
hasattr
(
self
,
'parent'
):
return
self
.
parent
.
get
(
attr
,
default
)
else
:
return
default
\ No newline at end of file
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