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
ec120d99
Commit
ec120d99
authored
13 years ago
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
The View constructor now accepts a load_template.
Added a test case to load templates from a dictionary.
parent
a4527728
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
12 deletions
+29
-12
pystache/view.py
+16
-12
tests/test_view.py
+13
-0
No files found.
pystache/view.py
View file @
ec120d99
...
...
@@ -42,15 +42,17 @@ class View(object):
template_encoding
=
None
template_extension
=
'mustache'
template_loader
=
None
# A function that accepts a single template_name parameter.
_load_template
=
None
def
__init__
(
self
,
template
=
None
,
context
=
None
,
load
er
=
None
,
**
kwargs
):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
load
_template
=
None
,
**
kwargs
):
"""
Construct a View instance.
"""
# TODO: add a unit test for passing a loader.
self
.
template_loader
=
loader
if
load_template
is
not
None
:
self
.
_load_template
=
load_template
self
.
template
=
template
context
=
context
or
{}
...
...
@@ -71,14 +73,16 @@ class View(object):
return
attr
def
load_template
(
self
,
template_name
):
if
self
.
template_loader
is
None
:
# We delay setting the loader until now to allow users to set
# the template_extension attribute, etc. after View.__init__()
# has already been called.
self
.
template_loader
=
Loader
(
search_dirs
=
self
.
template_path
,
encoding
=
self
.
template_encoding
,
extension
=
self
.
template_extension
)
return
self
.
template_loader
.
load_template
(
template_name
)
if
self
.
_load_template
is
None
:
# We delay setting self._load_template until now (in the case
# that the user did not supply a load_template to the constructor)
# to let users set the template_extension attribute, etc. after
# View.__init__() has already been called.
loader
=
Loader
(
search_dirs
=
self
.
template_path
,
encoding
=
self
.
template_encoding
,
extension
=
self
.
template_extension
)
self
.
_load_template
=
loader
.
load_template
return
self
.
_load_template
(
template_name
)
def
get_template
(
self
):
"""
...
...
This diff is collapsed.
Click to expand it.
tests/test_view.py
View file @
ec120d99
...
...
@@ -30,6 +30,19 @@ class TestView(unittest.TestCase):
template
=
Simple
()
.
load_template
(
"escaped"
)
self
.
assertEquals
(
template
,
"<h1>{{title}}</h1>"
)
def
test_custom_load_template
(
self
):
"""
Test passing a custom load_template to View.__init__().
"""
partials_dict
=
{
"partial"
:
"Loaded from dictionary"
}
load_template
=
lambda
template_name
:
partials_dict
[
template_name
]
view
=
Simple
(
load_template
=
load_template
)
actual
=
view
.
load_template
(
"partial"
)
self
.
assertEquals
(
actual
,
"Loaded from dictionary"
)
def
test_template_load_from_multiple_path
(
self
):
path
=
Simple
.
template_path
Simple
.
template_path
=
(
'examples/nowhere'
,
'examples'
,)
...
...
This diff is collapsed.
Click to expand it.
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