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
bcd4afe9
Commit
bcd4afe9
authored
Dec 17, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
View class now uses new Context class.
parent
c689ce93
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
64 deletions
+12
-64
pystache/template.py
+3
-3
pystache/view.py
+9
-55
tests/test_view.py
+0
-6
No files found.
pystache/template.py
View file @
bcd4afe9
...
@@ -158,12 +158,12 @@ class Template(object):
...
@@ -158,12 +158,12 @@ class Template(object):
return
template
return
template
def
_render_dictionary
(
self
,
template
,
context
):
def
_render_dictionary
(
self
,
template
,
context
):
self
.
view
.
context
_list
.
insert
(
0
,
context
)
self
.
view
.
context
.
push
(
context
)
template
=
Template
(
template
,
self
.
view
)
template
=
Template
(
template
,
self
.
view
)
out
=
template
.
render
()
out
=
template
.
render
()
self
.
view
.
context
_list
.
pop
(
0
)
self
.
view
.
context
.
pop
(
)
return
out
return
out
...
@@ -186,7 +186,7 @@ class Template(object):
...
@@ -186,7 +186,7 @@ class Template(object):
# See issue #34: https://github.com/defunkt/pystache/issues/34
# See issue #34: https://github.com/defunkt/pystache/issues/34
if
not
raw
and
raw
!=
0
:
if
not
raw
and
raw
!=
0
:
if
tag_name
==
'.'
:
if
tag_name
==
'.'
:
raw
=
self
.
view
.
context
_list
[
0
]
raw
=
self
.
view
.
context
.
top
()
else
:
else
:
return
''
return
''
...
...
pystache/view.py
View file @
bcd4afe9
...
@@ -8,32 +8,11 @@ This module provides a View class.
...
@@ -8,32 +8,11 @@ This module provides a View class.
import
re
import
re
from
types
import
UnboundMethodType
from
types
import
UnboundMethodType
from
.context
import
Context
from
.loader
import
Loader
from
.loader
import
Loader
from
.template
import
Template
from
.template
import
Template
def
get_or_attr
(
context_list
,
name
,
default
=
None
):
"""
Find and return an attribute from the given context.
"""
if
not
context_list
:
return
default
for
obj
in
context_list
:
try
:
return
obj
[
name
]
except
KeyError
:
pass
except
:
try
:
return
getattr
(
obj
,
name
)
except
AttributeError
:
pass
return
default
class
View
(
object
):
class
View
(
object
):
template_name
=
None
template_name
=
None
...
@@ -50,31 +29,19 @@ class View(object):
...
@@ -50,31 +29,19 @@ class View(object):
Construct a View instance.
Construct a View instance.
"""
"""
if
context
is
None
:
context
=
{}
if
load_template
is
not
None
:
if
load_template
is
not
None
:
self
.
_load_template
=
load_template
self
.
_load_template
=
load_template
if
template
is
not
None
:
if
template
is
not
None
:
self
.
template
=
template
self
.
template
=
template
context
=
context
or
{}
_context
=
Context
(
self
)
context
.
update
(
**
kwargs
)
if
context
:
_context
.
push
(
context
)
self
.
context_list
=
[
context
]
if
kwargs
:
_context
.
push
(
kwargs
)
def
get
(
self
,
attr
,
default
=
None
):
self
.
context
=
_context
"""
Return the value for the given attribute.
"""
attr
=
get_or_attr
(
self
.
context_list
,
attr
,
getattr
(
self
,
attr
,
default
))
if
hasattr
(
attr
,
'__call__'
)
and
type
(
attr
)
is
UnboundMethodType
:
return
attr
()
else
:
return
attr
def
load_template
(
self
,
template_name
):
def
load_template
(
self
,
template_name
):
if
self
.
_load_template
is
None
:
if
self
.
_load_template
is
None
:
...
@@ -121,13 +88,6 @@ class View(object):
...
@@ -121,13 +88,6 @@ class View(object):
return
re
.
sub
(
'[A-Z]'
,
repl
,
template_name
)[
1
:]
return
re
.
sub
(
'[A-Z]'
,
repl
,
template_name
)[
1
:]
def
_get_context
(
self
):
context
=
{}
for
item
in
self
.
context_list
:
if
hasattr
(
item
,
'keys'
)
and
hasattr
(
item
,
'__getitem__'
):
context
.
update
(
item
)
return
context
def
render
(
self
,
encoding
=
None
):
def
render
(
self
,
encoding
=
None
):
"""
"""
Return the view rendered using the current context.
Return the view rendered using the current context.
...
@@ -136,14 +96,8 @@ class View(object):
...
@@ -136,14 +96,8 @@ class View(object):
template
=
Template
(
self
.
get_template
(),
self
)
template
=
Template
(
self
.
get_template
(),
self
)
return
template
.
render
(
encoding
=
encoding
)
return
template
.
render
(
encoding
=
encoding
)
def
__contains__
(
self
,
needle
):
def
get
(
self
,
key
,
default
=
None
):
return
needle
in
self
.
context
or
hasattr
(
self
,
needle
)
return
self
.
context
.
get
(
key
,
default
)
def
__getattr__
(
self
,
attr
):
if
attr
==
'context'
:
return
self
.
_get_context
()
raise
AttributeError
(
"Attribute '
%
s' does not exist in View"
%
attr
)
def
__str__
(
self
):
def
__str__
(
self
):
return
self
.
render
()
return
self
.
render
()
tests/test_view.py
View file @
bcd4afe9
...
@@ -173,12 +173,6 @@ class ViewTestCase(unittest.TestCase):
...
@@ -173,12 +173,6 @@ class ViewTestCase(unittest.TestCase):
self
.
assertEquals
(
view
.
render
(),
'derp'
)
self
.
assertEquals
(
view
.
render
(),
'derp'
)
def
test_context_returns_a_flattened_dict
(
self
):
view
=
Simple
()
view
.
context_list
=
[{
'one'
:
'1'
},
{
'two'
:
'2'
},
object
()]
self
.
assertEqual
(
view
.
context
,
{
'one'
:
'1'
,
'two'
:
'2'
})
def
test_inverted_lists
(
self
):
def
test_inverted_lists
(
self
):
view
=
InvertedLists
()
view
=
InvertedLists
()
self
.
assertEquals
(
view
.
render
(),
"""one, two, three, empty list"""
)
self
.
assertEquals
(
view
.
render
(),
"""one, two, three, empty list"""
)
...
...
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