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
c859dfa2
Commit
c859dfa2
authored
Jan 12, 2011
by
Carl Whittaker
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding support for template encoding and support for comment and delimeter tags.
parent
28e63d4f
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
16 deletions
+65
-16
pystache/loader.py
+8
-2
pystache/template.py
+30
-3
pystache/view.py
+9
-9
tests/test_simple.py
+18
-2
No files found.
pystache/loader.py
View file @
c859dfa2
...
...
@@ -6,11 +6,17 @@ class Loader(object):
template_path
=
'.'
template_encoding
=
None
def
load_template
(
self
,
template_name
,
template_dirs
=
None
):
def
load_template
(
self
,
template_name
,
template_dirs
=
None
,
encoding
=
None
,
extension
=
None
):
'''Returns the template string from a file or throws IOError if it non existent'''
if
None
==
template_dirs
:
template_dirs
=
self
.
template_path
if
encoding
is
not
None
:
self
.
template_encoding
=
encoding
if
extension
is
not
None
:
self
.
template_extension
=
extension
file_name
=
template_name
+
'.'
+
self
.
template_extension
# Given a single directory we'll load from it
...
...
pystache/template.py
View file @
c859dfa2
...
...
@@ -109,6 +109,7 @@ class Template(object):
for
item
in
listing
:
view
=
View
(
context
=
item
)
view
.
template_path
=
self
.
view
.
template_path
view
.
template_encoding
=
self
.
view
.
template_encoding
view
.
parent
=
self
.
view
insides
.
append
(
Template
(
template
,
view
)
.
render
())
...
...
@@ -118,16 +119,41 @@ class Template(object):
def
_render_tag
(
self
,
tag_name
,
view
):
raw
=
view
.
get
(
tag_name
,
''
)
# For methods with no return value
if
not
raw
and
raw
is
not
0
:
return
''
return
cgi
.
escape
(
unicode
(
raw
))
@modifier
(
'!'
)
def
_render_comment
(
self
,
tag_name
,
view
):
return
''
@modifier
(
'>'
)
def
_render_partial
(
self
,
template_name
,
view
):
# mothereffin template loader goes here
template
=
view
.
get_template
(
template_name
)
from
pystache
import
Loader
template
=
Loader
()
.
load_template
(
template_name
,
view
.
template_path
,
encoding
=
view
.
template_encoding
)
return
Template
(
template
,
view
)
.
render
()
def
render
(
self
):
@modifier
(
'='
)
def
_change_delimiter
(
self
,
tag_name
,
view
):
"""Changes the Mustache delimiter."""
self
.
otag
,
self
.
ctag
=
tag_name
.
split
(
' '
)
self
.
_compile_regexps
()
return
''
@modifier
(
'{'
)
@modifier
(
'&'
)
def
render_unescaped
(
self
,
tag_name
,
view
):
"""Render a tag without escaping it."""
return
unicode
(
view
.
get
(
tag_name
,
''
))
def
render
(
self
,
encoding
=
None
):
template
=
self
.
_render_sections
(
self
.
template
,
self
.
view
)
result
=
self
.
_render_tags
(
template
,
self
.
view
)
if
encoding
is
not
None
:
result
=
result
.
encode
(
encoding
)
return
result
\ No newline at end of file
pystache/view.py
View file @
c859dfa2
...
...
@@ -8,6 +8,8 @@ class View(object):
template_name
=
None
template_path
=
None
template
=
None
template_encoding
=
None
template_extension
=
None
def
__init__
(
self
,
template
=
None
,
context
=
None
,
**
kwargs
):
self
.
template
=
template
...
...
@@ -16,7 +18,6 @@ class View(object):
def
get
(
self
,
attr
,
default
=
None
):
attr
=
self
.
context
.
get
(
attr
,
getattr
(
self
,
attr
,
self
.
_get_from_parent
(
attr
,
default
)))
if
hasattr
(
attr
,
'__call__'
)
and
type
(
attr
)
is
UnboundMethodType
:
return
attr
()
else
:
...
...
@@ -32,7 +33,7 @@ class View(object):
if
not
self
.
template
:
from
pystache
import
Loader
template_name
=
self
.
_get_template_name
(
template_name
)
self
.
template
=
Loader
()
.
load_template
(
template_name
,
self
.
template_path
)
self
.
template
=
Loader
()
.
load_template
(
template_name
,
self
.
template_path
,
encoding
=
self
.
template_encoding
,
extension
=
self
.
template_extension
)
return
self
.
template
...
...
@@ -41,16 +42,15 @@ class View(object):
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
template_name
:
return
template_name
if
not
template_name
:
template_name
=
self
.
__class__
.
__name__
template_name
=
self
.
__class__
.
__name__
def
repl
(
match
):
return
'_'
+
match
.
group
(
0
)
.
lower
()
return
re
.
sub
(
'[A-Z]'
,
repl
,
template_name
)[
1
:]
def
render
(
self
):
return
Template
(
self
.
get_template
(
self
.
template_name
),
self
)
.
render
()
\ No newline at end of file
def
render
(
self
,
encoding
=
None
):
return
Template
(
self
.
get_template
(
self
.
template_name
),
self
)
.
render
(
encoding
=
encoding
)
\ No newline at end of file
tests/test_simple.py
View file @
c859dfa2
...
...
@@ -4,6 +4,7 @@ from examples.nested_context import NestedContext
from
examples.complex_view
import
ComplexView
from
examples.lambdas
import
Lambdas
from
examples.template_partial
import
TemplatePartial
from
examples.simple
import
Simple
class
TestSimple
(
unittest
.
TestCase
):
...
...
@@ -30,4 +31,19 @@ class TestSimple(unittest.TestCase):
view
=
TemplatePartial
()
self
.
assertEquals
(
pystache
.
Template
(
'{{>inner_partial}}'
,
view
)
.
render
(),
'Again, Welcome!'
)
self
.
assertEquals
(
pystache
.
Template
(
'{{#looping}}{{>inner_partial}} {{/looping}}'
,
view
)
.
render
(),
'Again, Welcome! Again, Welcome! Again, Welcome! '
)
\ No newline at end of file
self
.
assertEquals
(
pystache
.
Template
(
'{{#looping}}{{>inner_partial}} {{/looping}}'
,
view
)
.
render
(),
'''Again, Welcome! Again, Welcome! Again, Welcome!'''
)
def
test_non_existent_value_renders_blank
(
self
):
view
=
Simple
()
self
.
assertEquals
(
pystache
.
Template
(
'{{not_set}} {{blank}}'
,
view
)
.
render
(),
' '
)
def
test_template_partial_extension
(
self
):
view
=
TemplatePartial
()
view
.
template_extension
=
'txt'
self
.
assertEquals
(
view
.
render
(),
"""Welcome
-------
Again, Welcome!
"""
)
\ No newline at end of file
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