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
40b904a3
Commit
40b904a3
authored
Nov 12, 2009
by
Chris Wanstrath
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement partials, make test pass
parent
d02b657f
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
7 deletions
+48
-7
pystache/template.py
+13
-2
pystache/view.py
+33
-4
tests/test_examples.py
+2
-1
No files found.
pystache/template.py
View file @
40b904a3
...
@@ -8,7 +8,8 @@ class Template(object):
...
@@ -8,7 +8,8 @@ class Template(object):
tag_types
=
{
tag_types
=
{
None
:
'tag'
,
None
:
'tag'
,
'!'
:
'comment'
,
'!'
:
'comment'
,
'{'
:
'unescaped'
'{'
:
'unescaped'
,
'>'
:
'partial'
}
}
def
__init__
(
self
,
template
,
context
=
None
):
def
__init__
(
self
,
template
,
context
=
None
):
...
@@ -63,7 +64,7 @@ class Template(object):
...
@@ -63,7 +64,7 @@ class Template(object):
return
template
return
template
def
render_tag
(
self
,
tag_name
,
context
):
def
render_tag
(
self
,
tag_name
,
context
):
"""Given a tag name and context, finds and renders the tag."""
"""Given a tag name and context, finds
, escapes,
and renders the tag."""
return
cgi
.
escape
(
context
.
get
(
tag_name
,
''
))
return
cgi
.
escape
(
context
.
get
(
tag_name
,
''
))
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
def
render_comment
(
self
,
tag_name
=
None
,
context
=
None
):
...
@@ -73,3 +74,13 @@ class Template(object):
...
@@ -73,3 +74,13 @@ class Template(object):
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
context
.
get
(
tag_name
,
''
)
return
context
.
get
(
tag_name
,
''
)
def
render_partial
(
self
,
tag_name
=
None
,
context
=
None
):
"""Renders a partial within the current context."""
# Import view here to avoid import loop
from
pystache.view
import
View
view
=
View
(
context
=
context
)
view
.
template_name
=
tag_name
return
view
.
render
()
pystache/view.py
View file @
40b904a3
...
@@ -9,6 +9,10 @@ class View(object):
...
@@ -9,6 +9,10 @@ class View(object):
# Extension for templates
# Extension for templates
template_extension
=
'mustache'
template_extension
=
'mustache'
# The name of this template. If none is given the View will try
# to infer it based on the class name.
template_name
=
None
# Absolute path to the template itself. Pystache will try to guess
# Absolute path to the template itself. Pystache will try to guess
# if it's not provided.
# if it's not provided.
template_file
=
None
template_file
=
None
...
@@ -19,14 +23,35 @@ class View(object):
...
@@ -19,14 +23,35 @@ class View(object):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
self
.
template
=
template
self
.
template
=
template
self
.
context
=
context
or
{}
self
.
context
=
context
or
{}
self
.
context
.
update
(
kwargs
)
# If the context we're handed is a View, we want to inherit
# its settings.
if
isinstance
(
context
,
View
):
self
.
inherit_settings
(
context
)
if
kwargs
:
self
.
context
.
update
(
kwargs
)
def
inherit_settings
(
self
,
view
):
"""Given another View, copies its settings."""
if
view
.
template_path
:
self
.
template_path
=
view
.
template_path
if
view
.
template_name
:
self
.
template_name
=
view
.
template_name
def
__contains__
(
self
,
needle
):
return
hasattr
(
self
,
needle
)
def
__getitem__
(
self
,
attr
):
return
getattr
(
self
,
attr
)()
def
load_template
(
self
):
def
load_template
(
self
):
if
self
.
template
:
if
self
.
template
:
return
self
.
template
return
self
.
template
if
not
self
.
template_file
:
if
not
self
.
template_file
:
name
=
self
.
template_name
()
+
'.'
+
self
.
template_extension
name
=
self
.
get_
template_name
()
+
'.'
+
self
.
template_extension
self
.
template_file
=
os
.
path
.
join
(
self
.
template_path
,
name
)
self
.
template_file
=
os
.
path
.
join
(
self
.
template_path
,
name
)
f
=
open
(
self
.
template_file
,
'r'
)
f
=
open
(
self
.
template_file
,
'r'
)
...
@@ -34,10 +59,14 @@ class View(object):
...
@@ -34,10 +59,14 @@ class View(object):
f
.
close
()
f
.
close
()
return
template
return
template
def
template_name
(
self
,
name
=
None
):
def
get_
template_name
(
self
,
name
=
None
):
"""TemplatePartial => template_partial
"""TemplatePartial => template_partial
Takes a string but defaults to using the current class' name.
Takes a string but defaults to using the current class' name or
the `template_name` attribute
"""
"""
if
self
.
template_name
:
return
self
.
template_name
if
not
name
:
if
not
name
:
name
=
self
.
__class__
.
__name__
name
=
self
.
__class__
.
__name__
...
...
tests/test_examples.py
View file @
40b904a3
...
@@ -24,4 +24,5 @@ class TestView(unittest.TestCase):
...
@@ -24,4 +24,5 @@ class TestView(unittest.TestCase):
self
.
assertEquals
(
Unescaped
()
.
render
(),
"<h1>Bear > Shark</h1>"
)
self
.
assertEquals
(
Unescaped
()
.
render
(),
"<h1>Bear > Shark</h1>"
)
def
test_template_partial
(
self
):
def
test_template_partial
(
self
):
self
.
assertEquals
(
TemplatePartial
()
.
render
(),
'blah'
)
self
.
assertEquals
(
TemplatePartial
()
.
render
(),
"""<h1>Welcome</h1>
Again, Welcome!"""
)
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