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
aaed5225
Commit
aaed5225
authored
Dec 27, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed the loader argument of Renderer.__init__() to "partials".
parent
e256e732
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
34 additions
and
39 deletions
+34
-39
pystache/renderer.py
+19
-21
pystache/view.py
+1
-1
tests/test_renderer.py
+14
-17
No files found.
pystache/renderer.py
View file @
aaed5225
...
...
@@ -29,38 +29,37 @@ class Renderer(object):
This class supports several rendering options which are described in
the constructor's docstring. Among these, the constructor supports
passing a custom
template
loader.
passing a custom
partial
loader.
Here is an example of
passing a custom template loader to render a
t
emplate using
partials loaded from a string-string dictionary.
Here is an example of
rendering a template using a custom partial loader
t
hat loads
partials loaded from a string-string dictionary.
>>> partials = {'partial': 'Hello, {{thing}}!'}
>>> renderer = Renderer(
loader
=partials)
>>> renderer = Renderer(
partials
=partials)
>>> renderer.render('{{>partial}}', {'thing': 'world'})
u'Hello, world!'
"""
# TODO: rename the loader argument to "partials".
def
__init__
(
self
,
loader
=
None
,
file_encoding
=
None
,
default_encoding
=
None
,
def
__init__
(
self
,
file_encoding
=
None
,
default_encoding
=
None
,
decode_errors
=
'strict'
,
search_dirs
=
None
,
file_extension
=
None
,
escape
=
None
):
escape
=
None
,
partials
=
None
):
"""
Construct an instance.
Arguments:
loader: an object (e.g. pystache.Loader or dictionary) for custom
partial loading during the rendering process.
The
loader
should have a get() method that accepts a string
partials: an object (e.g. pystache.Loader or dictionary) for
custom
partial loading during the rendering process.
The
object
should have a get() method that accepts a string
and returns the corresponding template as a string, preferably
as a unicode string. If there is no template with that name,
the
method should either return None (as dict.get() does) or
raise an exception.
If this argument is None,
partial loading takes place using
the normal procedure of
reading templates from the file syste
m
using the Loader-related instance attributes (search_dirs,
file_encoding, etc)
.
the
get() method should either return None (as dict.get() does)
or
raise an exception.
If this argument is None,
the rendering process will use
the normal procedure of
locating and reading templates fro
m
the file system -- using the Loader-related instance attributes
like search_dirs, file_encoding, etc
.
escape: the function used to escape variable tag values when
rendering a template. The function should accept a unicode
...
...
@@ -124,8 +123,7 @@ class Renderer(object):
self
.
escape
=
escape
self
.
file_encoding
=
file_encoding
self
.
file_extension
=
file_extension
# TODO: rename self.loader to self.partials.
self
.
loader
=
loader
self
.
partials
=
partials
self
.
search_dirs
=
search_dirs
def
_to_unicode_soft
(
self
,
s
):
...
...
@@ -209,17 +207,17 @@ class Renderer(object):
Return the load_partial function to pass to RenderEngine.__init__().
"""
if
self
.
loader
is
None
:
if
self
.
partials
is
None
:
loader
=
self
.
_make_loader
()
return
loader
.
get
# Otherwise, create a load_partial function from the custom loader
# that satisfies RenderEngine requirements (and that provides a
# nicer exception, etc).
loader
=
self
.
loader
get_partial
=
self
.
partials
.
get
def
load_partial
(
name
):
template
=
loader
.
get
(
name
)
template
=
get_partial
(
name
)
if
template
is
None
:
# TODO: make a TemplateNotFoundException type that provides
...
...
pystache/view.py
View file @
aaed5225
...
...
@@ -60,7 +60,7 @@ class View(object):
# instantiation some of the attributes on which the Renderer
# depends. This lets users set the template_extension attribute,
# etc. after View.__init__() has already been called.
renderer
=
Renderer
(
loader
=
self
.
_loader
,
renderer
=
Renderer
(
partials
=
self
.
_loader
,
file_encoding
=
self
.
template_encoding
,
search_dirs
=
self
.
template_path
,
file_extension
=
self
.
template_extension
)
...
...
tests/test_renderer.py
View file @
aaed5225
...
...
@@ -23,24 +23,21 @@ class RendererInitTestCase(unittest.TestCase):
"""
def
test_
loader
(
self
):
def
test_
partials__default
(
self
):
"""
Test that the
loader attribute is set
correctly.
Test that the
default loader is constructed
correctly.
"""
loader
=
{
'foo'
:
'bar'
}
renderer
=
Renderer
(
loader
=
loader
)
self
.
assertEquals
(
renderer
.
loader
,
{
'foo'
:
'bar'
})
renderer
=
Renderer
()
self
.
assertTrue
(
renderer
.
partials
is
None
)
def
test_
loader__default
(
self
):
def
test_
partials
(
self
):
"""
Test that the
default loader is constructed
correctly.
Test that the
loader attribute is set
correctly.
"""
renderer
=
Renderer
()
actual
=
renderer
.
loader
self
.
assertTrue
(
renderer
.
loader
is
None
)
renderer
=
Renderer
(
partials
=
{
'foo'
:
'bar'
})
self
.
assertEquals
(
renderer
.
partials
,
{
'foo'
:
'bar'
})
def
test_escape__default
(
self
):
escape
=
Renderer
()
.
escape
...
...
@@ -382,8 +379,8 @@ class RendererTestCase(unittest.TestCase):
Test the _make_load_partial() method.
"""
partials
=
{
'foo'
:
'bar'
}
renderer
=
Renderer
(
loader
=
partials
)
renderer
=
Renderer
()
renderer
.
partials
=
{
'foo'
:
'bar'
}
load_partial
=
renderer
.
_make_load_partial
()
actual
=
load_partial
(
'foo'
)
...
...
@@ -398,12 +395,12 @@ class RendererTestCase(unittest.TestCase):
"""
renderer
=
Renderer
()
renderer
.
loader
=
{
'partial'
:
'foo'
}
renderer
.
partials
=
{
'partial'
:
'foo'
}
load_partial
=
renderer
.
_make_load_partial
()
self
.
assertEquals
(
load_partial
(
"partial"
),
"foo"
)
# Now with a value that is already unicode.
renderer
.
loader
=
{
'partial'
:
u'foo'
}
renderer
.
partials
=
{
'partial'
:
u'foo'
}
load_partial
=
renderer
.
_make_load_partial
()
# If the next line failed, we would get the following error:
# TypeError: decoding Unicode is not supported
...
...
@@ -443,7 +440,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
renderer
=
Renderer
()
renderer
.
default_encoding
=
'ascii'
renderer
.
loader
=
{
'str'
:
'foo'
,
'subclass'
:
MyUnicode
(
'abc'
)}
renderer
.
partials
=
{
'str'
:
'foo'
,
'subclass'
:
MyUnicode
(
'abc'
)}
engine
=
renderer
.
_make_render_engine
()
...
...
@@ -462,7 +459,7 @@ class Renderer_MakeRenderEngineTests(unittest.TestCase):
"""
renderer
=
Renderer
()
renderer
.
loader
=
{}
renderer
.
partials
=
{}
engine
=
renderer
.
_make_render_engine
()
load_partial
=
engine
.
load_partial
...
...
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