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
ab31eadc
Commit
ab31eadc
authored
Nov 12, 2009
by
Chris Wanstrath
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
delimiter support
parent
76b3c5d1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
6 deletions
+64
-6
examples/delimiters.mustache
+6
-0
examples/delimiters.py
+13
-0
pystache/template.py
+34
-6
tests/test_examples.py
+11
-0
No files found.
examples/delimiters.mustache
0 → 100644
View file @
ab31eadc
{{
=
<%
%>
=
}}
*
<
%
first
%
>
<
%=|
|=%
>
* | second |
|=
{{
}}
=|
*
{{
third
}}
examples/delimiters.py
0 → 100644
View file @
ab31eadc
import
pystache
class
Delimiters
(
pystache
.
View
):
template_path
=
'examples'
def
first
(
self
):
return
"It worked the first time."
def
second
(
self
):
return
"And it worked the second time."
def
third
(
self
):
return
"Then, surprisingly, it worked the third time."
pystache/template.py
View file @
ab31eadc
import
re
import
cgi
SECTION_RE
=
re
.
compile
(
r"{{\#([^\}]*)}}\s*(.+?)\s*{{/\1}}"
,
re
.
M
|
re
.
S
)
TAG_RE
=
re
.
compile
(
r"{{(#|=|!|<|>|\{)?(.+?)\1?}}+"
)
class
Template
(
object
):
# The regular expression used to find a #section
section_re
=
None
# The regular expression used to find a tag.
tag_re
=
None
# Opening tag delimiter
otag
=
'{{'
# Closing tag delimiter
ctag
=
'}}'
# Names of different tag modifiers, used to render them.
tag_types
=
{
None
:
'tag'
,
'!'
:
'comment'
,
'{'
:
'unescaped'
,
'>'
:
'partial'
'>'
:
'partial'
,
'='
:
'delimiter'
}
def
__init__
(
self
,
template
,
context
=
None
):
self
.
template
=
template
self
.
context
=
context
or
{}
self
.
compile_regexps
()
def
render
(
self
,
template
=
None
,
context
=
None
):
"""Turns a Mustache template into something wonderful."""
...
...
@@ -24,10 +36,20 @@ class Template(object):
template
=
self
.
render_sections
(
template
,
context
)
return
self
.
render_tags
(
template
,
context
)
def
compile_regexps
(
self
):
"""Compiles our section and tag regular expressions."""
tags
=
{
'otag'
:
re
.
escape
(
self
.
otag
),
'ctag'
:
re
.
escape
(
self
.
ctag
)
}
section
=
r"
%(otag)
s\#([^\}]*)
%(ctag)
s\s*(.+?)\s*
%(otag)
s/\1
%(ctag)
s"
self
.
section_re
=
re
.
compile
(
section
%
tags
,
re
.
M
|
re
.
S
)
tag
=
r"
%(otag)
s(#|=|!|>|\{)?\s*(.+?)\s*\1?
%(ctag)
s+"
self
.
tag_re
=
re
.
compile
(
tag
%
tags
)
def
render_sections
(
self
,
template
,
context
):
"""Expands sections."""
while
1
:
match
=
SECTION_RE
.
search
(
template
)
match
=
self
.
section_re
.
search
(
template
)
if
match
is
None
:
break
...
...
@@ -50,7 +72,7 @@ class Template(object):
def
render_tags
(
self
,
template
,
context
):
"""Renders all the tags in a template for a context."""
while
1
:
match
=
TAG_RE
.
search
(
template
)
match
=
self
.
tag_re
.
search
(
template
)
if
match
is
None
:
break
...
...
@@ -84,3 +106,9 @@ class Template(object):
view
.
template_name
=
tag_name
return
view
.
render
()
def
render_delimiter
(
self
,
tag_name
=
None
,
context
=
None
):
"""Changes the Mustache delimiter."""
self
.
otag
,
self
.
ctag
=
tag_name
.
split
(
' '
)
self
.
compile_regexps
()
return
''
tests/test_examples.py
View file @
ab31eadc
...
...
@@ -6,6 +6,7 @@ from examples.double_section import DoubleSection
from
examples.escaped
import
Escaped
from
examples.unescaped
import
Unescaped
from
examples.template_partial
import
TemplatePartial
from
examples.delimiters
import
Delimiters
class
TestView
(
unittest
.
TestCase
):
def
test_comments
(
self
):
...
...
@@ -35,3 +36,13 @@ Again, Welcome!""")
Again, Welcome!
"""
)
def
test_delimiters
(
self
):
self
.
assertEquals
(
Delimiters
()
.
render
(),
"""
* It worked the first time.
* And it worked the second time.
* Then, surprisingly, it worked the third time.
"""
)
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