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
120d4225
Commit
120d4225
authored
Dec 18, 2011
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added to the Template class support for disabling HTML escaping.
parent
fd2f53da
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
4 deletions
+56
-4
pystache/template.py
+14
-4
tests/test_template.py
+42
-0
No files found.
pystache/template.py
View file @
120d4225
...
@@ -62,7 +62,8 @@ class Template(object):
...
@@ -62,7 +62,8 @@ class Template(object):
modifiers
=
Modifiers
()
modifiers
=
Modifiers
()
def
__init__
(
self
,
template
=
None
,
load_template
=
None
,
output_encoding
=
None
):
def
__init__
(
self
,
template
=
None
,
load_template
=
None
,
output_encoding
=
None
,
disable_escape
=
False
):
"""
"""
Construct a Template instance.
Construct a Template instance.
...
@@ -85,12 +86,19 @@ class Template(object):
...
@@ -85,12 +86,19 @@ class Template(object):
loader
=
Loader
()
loader
=
Loader
()
load_template
=
loader
.
load_template
load_template
=
loader
.
load_template
self
.
disable_escape
=
disable_escape
self
.
load_template
=
load_template
self
.
load_template
=
load_template
self
.
output_encoding
=
output_encoding
self
.
output_encoding
=
output_encoding
self
.
template
=
template
self
.
template
=
template
self
.
_compile_regexps
()
self
.
_compile_regexps
()
def
escape
(
self
,
text
):
return
escape
(
text
)
def
literal
(
self
,
text
):
return
literal
(
text
)
def
_initialize_context
(
self
,
context
,
**
kwargs
):
def
_initialize_context
(
self
,
context
,
**
kwargs
):
"""
"""
Initialize the context attribute.
Initialize the context attribute.
...
@@ -206,7 +214,7 @@ class Template(object):
...
@@ -206,7 +214,7 @@ class Template(object):
def
_render_dictionary
(
self
,
template
,
context
):
def
_render_dictionary
(
self
,
template
,
context
):
self
.
context
.
push
(
context
)
self
.
context
.
push
(
context
)
template
=
Template
(
template
,
load_template
=
self
.
load_template
)
template
=
Template
(
template
,
load_template
=
self
.
load_template
,
disable_escape
=
self
.
disable_escape
)
out
=
template
.
render
(
self
.
context
)
out
=
template
.
render
(
self
.
context
)
self
.
context
.
pop
()
self
.
context
.
pop
()
...
@@ -236,7 +244,7 @@ class Template(object):
...
@@ -236,7 +244,7 @@ class Template(object):
else
:
else
:
return
''
return
''
return
escap
e
(
raw
)
return
self
.
_render_valu
e
(
raw
)
@modifiers.set
(
'!'
)
@modifiers.set
(
'!'
)
def
_render_comment
(
self
,
tag_name
):
def
_render_comment
(
self
,
tag_name
):
...
@@ -245,7 +253,7 @@ class Template(object):
...
@@ -245,7 +253,7 @@ class Template(object):
@modifiers.set
(
'>'
)
@modifiers.set
(
'>'
)
def
_render_partial
(
self
,
template_name
):
def
_render_partial
(
self
,
template_name
):
markup
=
self
.
load_template
(
template_name
)
markup
=
self
.
load_template
(
template_name
)
template
=
Template
(
markup
,
load_template
=
self
.
load_template
)
template
=
Template
(
markup
,
load_template
=
self
.
load_template
,
disable_escape
=
self
.
disable_escape
)
return
template
.
render
(
self
.
context
)
return
template
.
render
(
self
.
context
)
@modifiers.set
(
'='
)
@modifiers.set
(
'='
)
...
@@ -286,6 +294,8 @@ class Template(object):
...
@@ -286,6 +294,8 @@ class Template(object):
"""
"""
self
.
_initialize_context
(
context
,
**
kwargs
)
self
.
_initialize_context
(
context
,
**
kwargs
)
self
.
_render_value
=
self
.
literal
if
self
.
disable_escape
else
self
.
escape
result
=
self
.
_render
(
self
.
template
)
result
=
self
.
_render
(
self
.
template
)
if
self
.
output_encoding
is
not
None
:
if
self
.
output_encoding
is
not
None
:
...
...
tests/test_template.py
View file @
120d4225
...
@@ -15,6 +15,14 @@ class TemplateTestCase(unittest.TestCase):
...
@@ -15,6 +15,14 @@ class TemplateTestCase(unittest.TestCase):
"""Test the Template class."""
"""Test the Template class."""
def
test_init__disable_escape
(
self
):
# Test default value.
template
=
Template
()
self
.
assertEquals
(
template
.
disable_escape
,
False
)
template
=
Template
(
disable_escape
=
True
)
self
.
assertEquals
(
template
.
disable_escape
,
True
)
def
test_render__unicode
(
self
):
def
test_render__unicode
(
self
):
template
=
Template
(
u'foo'
)
template
=
Template
(
u'foo'
)
actual
=
template
.
render
()
actual
=
template
.
render
()
...
@@ -117,3 +125,37 @@ class TemplateTestCase(unittest.TestCase):
...
@@ -117,3 +125,37 @@ class TemplateTestCase(unittest.TestCase):
context
=
{
'test'
:
(
lambda
text
:
'{{hi}}
%
s'
%
text
)}
context
=
{
'test'
:
(
lambda
text
:
'{{hi}}
%
s'
%
text
)}
actual
=
template
.
render
(
context
)
actual
=
template
.
render
(
context
)
self
.
assertEquals
(
actual
,
'{{hi}} Mom'
)
self
.
assertEquals
(
actual
,
'{{hi}} Mom'
)
def
test_render__html_escape
(
self
):
context
=
{
'test'
:
'1 < 2'
}
template
=
Template
(
'{{test}}'
)
self
.
assertEquals
(
template
.
render
(
context
),
'1 < 2'
)
def
test_render__html_escape_disabled
(
self
):
context
=
{
'test'
:
'1 < 2'
}
template
=
Template
(
'{{test}}'
)
self
.
assertEquals
(
template
.
render
(
context
),
'1 < 2'
)
template
.
disable_escape
=
True
self
.
assertEquals
(
template
.
render
(
context
),
'1 < 2'
)
def
test_render__html_escape_disabled_with_partial
(
self
):
context
=
{
'test'
:
'1 < 2'
}
load_template
=
lambda
name
:
'{{test}}'
template
=
Template
(
'{{>partial}}'
,
load_template
=
load_template
)
self
.
assertEquals
(
template
.
render
(
context
),
'1 < 2'
)
template
.
disable_escape
=
True
self
.
assertEquals
(
template
.
render
(
context
),
'1 < 2'
)
def
test_render__html_escape_disabled_with_non_false_value
(
self
):
context
=
{
'section'
:
{
'test'
:
'1 < 2'
}}
template
=
Template
(
'{{#section}}{{test}}{{/section}}'
)
self
.
assertEquals
(
template
.
render
(
context
),
'1 < 2'
)
template
.
disable_escape
=
True
self
.
assertEquals
(
template
.
render
(
context
),
'1 < 2'
)
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