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
2baff1a4
Commit
2baff1a4
authored
May 12, 2010
by
Paul Bonser
Committed by
Chris Wanstrath
May 15, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
get by key for dict-like objects, or try attribute access for other objects
parent
49497291
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
3 deletions
+16
-3
pystache/template.py
+16
-3
No files found.
pystache/template.py
View file @
2baff1a4
...
@@ -16,6 +16,19 @@ def modifier(symbol):
...
@@ -16,6 +16,19 @@ def modifier(symbol):
return
func
return
func
return
set_modifier
return
set_modifier
def
get_or_attr
(
obj
,
name
,
default
=
None
):
try
:
return
obj
[
name
]
except
KeyError
:
return
default
except
:
try
:
return
getattr
(
obj
,
name
)
except
AttributeError
:
return
default
class
Template
(
object
):
class
Template
(
object
):
# The regular expression used to find a #section
# The regular expression used to find a #section
section_re
=
None
section_re
=
None
...
@@ -65,7 +78,7 @@ class Template(object):
...
@@ -65,7 +78,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
=
context
.
get
(
section_name
,
None
)
it
=
get_or_attr
(
context
,
section_name
,
None
)
replacer
=
''
replacer
=
''
if
it
and
hasattr
(
it
,
'__call__'
):
if
it
and
hasattr
(
it
,
'__call__'
):
replacer
=
it
(
inner
)
replacer
=
it
(
inner
)
...
@@ -102,7 +115,7 @@ class Template(object):
...
@@ -102,7 +115,7 @@ class Template(object):
@modifier
(
None
)
@modifier
(
None
)
def
render_tag
(
self
,
tag_name
,
context
):
def
render_tag
(
self
,
tag_name
,
context
):
"""Given a tag name and context, finds, escapes, and renders the tag."""
"""Given a tag name and context, finds, escapes, and renders the tag."""
raw
=
context
.
get
(
tag_name
,
''
)
raw
=
get_or_attr
(
context
,
tag_name
,
''
)
if
not
raw
and
raw
is
not
0
:
if
not
raw
and
raw
is
not
0
:
return
''
return
''
return
cgi
.
escape
(
unicode
(
raw
))
return
cgi
.
escape
(
unicode
(
raw
))
...
@@ -116,7 +129,7 @@ class Template(object):
...
@@ -116,7 +129,7 @@ class Template(object):
@modifier
(
'&'
)
@modifier
(
'&'
)
def
render_unescaped
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_unescaped
(
self
,
tag_name
=
None
,
context
=
None
):
"""Render a tag without escaping it."""
"""Render a tag without escaping it."""
return
unicode
(
context
.
get
(
tag_name
,
''
))
return
unicode
(
get_or_attr
(
context
,
tag_name
,
''
))
@modifier
(
'>'
)
@modifier
(
'>'
)
def
render_partial
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_partial
(
self
,
tag_name
=
None
,
context
=
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