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
36e61711
Commit
36e61711
authored
Jan 12, 2011
by
Carl Whittaker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding support for nested contexts within View.
parent
8954e01f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
17 deletions
+38
-17
examples/nested_context.py
+10
-0
pystache/template.py
+8
-8
pystache/view.py
+15
-9
tests/test_examples.py
+5
-0
No files found.
examples/nested_context.py
View file @
36e61711
...
...
@@ -8,3 +8,12 @@ class NestedContext(pystache.View):
def
foo
(
self
):
return
{
'thing1'
:
'one'
,
'thing2'
:
'foo'
}
def
derp
(
self
):
return
[{
'inner'
:
'car'
}]
def
herp
(
self
):
return
[{
'outer'
:
'car'
}]
def
nested_context_in_view
(
self
):
return
'it works!'
if
self
[
'outer'
]
==
self
[
'inner'
]
else
''
\ No newline at end of file
pystache/template.py
View file @
36e61711
...
...
@@ -2,6 +2,7 @@ import re
import
cgi
import
collections
import
os
import
copy
modifiers
=
{}
def
modifier
(
symbol
):
...
...
@@ -96,15 +97,14 @@ class Template(object):
return
template
def
_render_dictionary
(
self
,
template
,
context
):
from
view
import
View
view
=
View
(
context
=
context
)
view
.
template_path
=
self
.
view
.
template_path
view
.
template_encoding
=
self
.
view
.
template_encoding
view
.
parent
=
self
.
view
return
Template
(
template
,
view
)
.
render
()
original_context
=
copy
.
copy
(
self
.
view
.
context
)
self
.
view
.
context
.
update
(
context
)
out
=
Template
(
template
,
self
.
view
)
.
render
()
self
.
view
.
context
=
original_context
return
out
def
_render_list
(
self
,
template
,
listing
):
def
_render_list
(
self
,
template
,
listing
):
insides
=
[]
for
item
in
listing
:
insides
.
append
(
self
.
_render_dictionary
(
template
,
item
))
...
...
pystache/view.py
View file @
36e61711
...
...
@@ -17,7 +17,7 @@ class View(object):
self
.
context
.
update
(
**
kwargs
)
def
get
(
self
,
attr
,
default
=
None
):
attr
=
self
.
context
.
get
(
attr
,
getattr
(
self
,
attr
,
self
.
_get_from_parent
(
attr
,
default
)
))
attr
=
self
.
context
.
get
(
attr
,
getattr
(
self
,
attr
,
default
))
if
hasattr
(
attr
,
'__call__'
)
and
type
(
attr
)
is
UnboundMethodType
:
return
attr
()
if
hasattr
(
attr
,
'render'
):
...
...
@@ -25,12 +25,6 @@ class View(object):
else
:
return
attr
def
_get_from_parent
(
self
,
attr
,
default
=
None
):
if
hasattr
(
self
,
'parent'
):
return
self
.
parent
.
get
(
attr
,
default
)
else
:
return
default
def
get_template
(
self
,
template_name
):
if
not
self
.
template
:
from
pystache
import
Loader
...
...
@@ -55,4 +49,16 @@ class View(object):
return
re
.
sub
(
'[A-Z]'
,
repl
,
template_name
)[
1
:]
def
render
(
self
,
encoding
=
None
):
return
Template
(
self
.
get_template
(
self
.
template_name
),
self
)
.
render
(
encoding
=
encoding
)
\ No newline at end of file
return
Template
(
self
.
get_template
(
self
.
template_name
),
self
)
.
render
(
encoding
=
encoding
)
def
__contains__
(
self
,
needle
):
return
needle
in
self
.
context
or
hasattr
(
self
,
needle
)
def
__getitem__
(
self
,
attr
):
val
=
self
.
get
(
attr
,
None
)
if
not
val
:
raise
KeyError
(
"No such key."
)
return
val
def
__str__
(
self
):
return
self
.
render
()
\ No newline at end of file
tests/test_examples.py
View file @
36e61711
...
...
@@ -71,5 +71,10 @@ Again, Welcome!
def
test_nested_context
(
self
):
self
.
assertEquals
(
NestedContext
()
.
render
(),
"one and foo and two"
)
def
test_nested_context_is_available_in_view
(
self
):
view
=
NestedContext
()
view
.
template
=
'{{#herp}}{{#derp}}{{nested_context_in_view}}{{/derp}}{{/herp}}'
self
.
assertEquals
(
view
.
render
(),
'it works!'
)
if
__name__
==
'__main__'
:
unittest
.
main
()
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