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
050ab67d
Commit
050ab67d
authored
Dec 17, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Completed issue #49: the Template class now manages the rendering context.
parent
bcd4afe9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
16 deletions
+29
-16
pystache/template.py
+29
-16
No files found.
pystache/template.py
View file @
050ab67d
...
@@ -9,6 +9,7 @@ import re
...
@@ -9,6 +9,7 @@ import re
import
cgi
import
cgi
import
collections
import
collections
from
.context
import
Context
from
.loader
import
Loader
from
.loader
import
Loader
...
@@ -63,24 +64,34 @@ class Template(object):
...
@@ -63,24 +64,34 @@ class Template(object):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
"""
"""
The **kwargs arguments are only supported if the context is
The context argument can be a dictionary, View, or Context instance.
a dictionary (i.e. not a View).
"""
"""
from
.view
import
View
from
.view
import
View
self
.
template
=
template
if
context
is
None
:
if
context
is
None
:
context
=
{}
context
=
{}
if
not
isinstance
(
context
,
View
):
view
=
None
# Views do not support copy() or update().
if
isinstance
(
context
,
View
):
view
=
context
context
=
view
.
context
.
copy
()
elif
isinstance
(
context
,
Context
):
context
=
context
.
copy
()
context
=
context
.
copy
()
view
=
View
(
context
=
context
,
**
kwargs
)
else
:
else
:
view
=
context
# Otherwise, the context is a dictionary.
context
=
Context
(
context
)
if
kwargs
:
context
.
push
(
kwargs
)
if
view
is
None
:
view
=
View
()
self
.
context
=
context
self
.
template
=
template
# The view attribute is used only for its load_template() method.
self
.
view
=
view
self
.
view
=
view
self
.
_compile_regexps
()
self
.
_compile_regexps
()
...
@@ -113,7 +124,7 @@ class Template(object):
...
@@ -113,7 +124,7 @@ class Template(object):
section
,
section_name
,
inner
=
match
.
group
(
0
,
1
,
2
)
section
,
section_name
,
inner
=
match
.
group
(
0
,
1
,
2
)
section_name
=
section_name
.
strip
()
section_name
=
section_name
.
strip
()
it
=
self
.
view
.
get
(
section_name
,
None
)
it
=
self
.
context
.
get
(
section_name
,
None
)
replacer
=
''
replacer
=
''
# Callable
# Callable
...
@@ -158,12 +169,13 @@ class Template(object):
...
@@ -158,12 +169,13 @@ class Template(object):
return
template
return
template
def
_render_dictionary
(
self
,
template
,
context
):
def
_render_dictionary
(
self
,
template
,
context
):
self
.
view
.
context
.
push
(
context
)
self
.
context
.
push
(
context
)
template
=
Template
(
template
,
self
.
view
)
template
=
Template
(
template
,
self
.
context
)
template
.
view
=
self
.
view
out
=
template
.
render
()
out
=
template
.
render
()
self
.
view
.
context
.
pop
()
self
.
context
.
pop
()
return
out
return
out
...
@@ -176,7 +188,7 @@ class Template(object):
...
@@ -176,7 +188,7 @@ class Template(object):
@modifiers.set
(
None
)
@modifiers.set
(
None
)
def
_render_tag
(
self
,
tag_name
):
def
_render_tag
(
self
,
tag_name
):
raw
=
self
.
view
.
get
(
tag_name
,
''
)
raw
=
self
.
context
.
get
(
tag_name
,
''
)
# For methods with no return value
# For methods with no return value
#
#
...
@@ -186,7 +198,7 @@ class Template(object):
...
@@ -186,7 +198,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
.
top
()
raw
=
self
.
context
.
top
()
else
:
else
:
return
''
return
''
...
@@ -199,7 +211,8 @@ class Template(object):
...
@@ -199,7 +211,8 @@ class Template(object):
@modifiers.set
(
'>'
)
@modifiers.set
(
'>'
)
def
_render_partial
(
self
,
template_name
):
def
_render_partial
(
self
,
template_name
):
markup
=
self
.
view
.
load_template
(
template_name
)
markup
=
self
.
view
.
load_template
(
template_name
)
template
=
Template
(
markup
,
self
.
view
)
template
=
Template
(
markup
,
self
.
context
)
template
.
view
=
self
.
view
return
template
.
render
()
return
template
.
render
()
@modifiers.set
(
'='
)
@modifiers.set
(
'='
)
...
@@ -220,7 +233,7 @@ class Template(object):
...
@@ -220,7 +233,7 @@ class Template(object):
Render a tag without escaping it.
Render a tag without escaping it.
"""
"""
return
literal
(
self
.
view
.
get
(
tag_name
,
''
))
return
literal
(
self
.
context
.
get
(
tag_name
,
''
))
def
render
(
self
,
encoding
=
None
):
def
render
(
self
,
encoding
=
None
):
"""
"""
...
...
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