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
5302934d
Commit
5302934d
authored
Dec 18, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved the encoding argument from Template.render() to Template.__init__().
parent
90a0ffc5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
14 deletions
+17
-14
pystache/template.py
+13
-11
pystache/view.py
+2
-2
tests/test_template.py
+2
-1
No files found.
pystache/template.py
View file @
5302934d
...
@@ -62,7 +62,7 @@ class Template(object):
...
@@ -62,7 +62,7 @@ class Template(object):
modifiers
=
Modifiers
()
modifiers
=
Modifiers
()
def
__init__
(
self
,
template
=
None
,
context
=
None
,
load_template
=
None
,
**
kwargs
):
def
__init__
(
self
,
template
=
None
,
context
=
None
,
load_template
=
None
,
output_encoding
=
None
,
**
kwargs
):
"""
"""
Construct a Template instance.
Construct a Template instance.
...
@@ -77,6 +77,11 @@ class Template(object):
...
@@ -77,6 +77,11 @@ class Template(object):
accept a single template_name parameter and return a template as
accept a single template_name parameter and return a template as
a string. Defaults to the default Loader's load_template() method.
a string. Defaults to the default Loader's load_template() method.
output_encoding: the encoding to use when rendering to a string.
The argument should be the name of an encoding as a string, for
example "utf-8". See the render() method's documentation for more
information.
"""
"""
if
context
is
None
:
if
context
is
None
:
context
=
{}
context
=
{}
...
@@ -96,6 +101,7 @@ class Template(object):
...
@@ -96,6 +101,7 @@ class Template(object):
self
.
context
=
context
self
.
context
=
context
self
.
load_template
=
load_template
self
.
load_template
=
load_template
self
.
output_encoding
=
output_encoding
self
.
template
=
template
self
.
template
=
template
self
.
_compile_regexps
()
self
.
_compile_regexps
()
...
@@ -237,23 +243,19 @@ class Template(object):
...
@@ -237,23 +243,19 @@ class Template(object):
"""
"""
return
literal
(
self
.
context
.
get
(
tag_name
,
''
))
return
literal
(
self
.
context
.
get
(
tag_name
,
''
))
def
render
(
self
,
encoding
=
None
):
def
render
(
self
):
"""
"""
Return the template rendered using the current context.
Return the template rendered using the current context.
The return value is a unicode string, unless the encoding argument
The return value is a unicode string, unless the output_encoding
is not None, in which case the return value has type str (encoded
attribute is not None, in which case the return value has type str
using that encoding).
and is encoded using that encoding.
Arguments:
encoding: the name of the encoding as a string, for example "utf-8".
"""
"""
template
=
self
.
_render_sections
(
self
.
template
)
template
=
self
.
_render_sections
(
self
.
template
)
result
=
self
.
_render_tags
(
template
)
result
=
self
.
_render_tags
(
template
)
if
encoding
is
not
None
:
if
self
.
output_
encoding
is
not
None
:
result
=
result
.
encode
(
encoding
)
result
=
result
.
encode
(
self
.
output_
encoding
)
return
result
return
result
pystache/view.py
View file @
5302934d
...
@@ -93,8 +93,8 @@ class View(object):
...
@@ -93,8 +93,8 @@ class View(object):
Return the view rendered using the current context.
Return the view rendered using the current context.
"""
"""
template
=
Template
(
self
.
get_template
(),
self
.
context
,
self
.
load_template
)
template
=
Template
(
self
.
get_template
(),
self
.
context
,
self
.
load_template
,
output_encoding
=
encoding
)
return
template
.
render
(
encoding
=
encoding
)
return
template
.
render
()
def
get
(
self
,
key
,
default
=
None
):
def
get
(
self
,
key
,
default
=
None
):
return
self
.
context
.
get
(
key
,
default
)
return
self
.
context
.
get
(
key
,
default
)
...
...
tests/test_template.py
View file @
5302934d
...
@@ -56,7 +56,8 @@ class TemplateTestCase(unittest.TestCase):
...
@@ -56,7 +56,8 @@ class TemplateTestCase(unittest.TestCase):
def
test_render__output_encoding
(
self
):
def
test_render__output_encoding
(
self
):
template
=
Template
(
u'Poincaré'
)
template
=
Template
(
u'Poincaré'
)
actual
=
template
.
render
(
'utf-8'
)
template
.
output_encoding
=
'utf-8'
actual
=
template
.
render
()
self
.
assertTrue
(
isinstance
(
actual
,
str
))
self
.
assertTrue
(
isinstance
(
actual
,
str
))
self
.
assertEquals
(
actual
,
'Poincaré'
)
self
.
assertEquals
(
actual
,
'Poincaré'
)
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