Skip to content

Commit

Permalink
Changes in template syntax: & -> $, etc.
Browse files Browse the repository at this point in the history
git-svn-id: http://pony.tigris.org/svn/pony/trunk@77 698dff77-f410-0410-8918-cf622e150f36
  • Loading branch information
kozlovsky committed Jan 24, 2007
1 parent a8d87eb commit be305bd
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 119 deletions.
14 changes: 7 additions & 7 deletions docs/template-css.txt
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
root {
<html>
<head>
<title>University &text{university/@name}</title>
<title>University $text{university/@name}</title>
<link rel="stylesheet" type="text/css" href="university.css" />
</head>
<body>
<h1>List of students:</h1>
&transform{university/groups/group}
$transform{university/groups/group}
</body>
</html>
}

group {
<div>
<h2>Group &text{@number}</h2>
<h2>Group $text{@number}</h2>
<p>
Count of students: &text{count(students)},<br>
among them &text{count(student[@type="contract"])} of contract />
Count of students: $text{count(students)},<br>
among them $text{count(student[@type="contract"])} of contract />
</p>
<ol>&transform{student}</ol>
<ol>$transform{student}</ol>
</div>
}

student { <li class="@type">&text{.}</li> }
student { <li class="@type">$text{.}</li> }
56 changes: 28 additions & 28 deletions docs/template-syntax.txt
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@

&&
&// comment
&/* comment */
$$
$// comment
$/* comment */

&if(expr) { text }
&elif(expr) { text }
&else { text }
$if(expr) { text }
$elif(expr) { text }
$else { text }

&for(var in expr) {
$for(var in expr) {
text
&row{text1}{text2}{text3}
&row(i,j){text}
&row("odd"){text} &// &row(1,2){text}
&row("even"){text} &// &row(2,2){text}
&row("first"){text}
&row("not first"){text}
&row("last"){text}
&row("not last"){text}
} &separator {
$row{text1}{text2}{text3}
$row(i,j){text}
$row("odd"){text} $// $row(1,2){text}
$row("even"){text} $// $row(2,2){text}
$row("first"){text}
$row("not first"){text}
$row("last"){text}
$row("not last"){text}
} $separator {
text
} &else {
} $else {
text
}

&{text} &// localization
${text} $// localization

&list(expr){text}
$list(expr){text}

&link(expr, attr=value, ...){text}
$link(expr, attr=value, ...){text}

------------------------------------------------

&myfunc(1, 2, a=3, b=4){ text1 }{ text2 }&c={ text3 }&d={ text4 }
$myfunc(1, 2, a=3, b=4){ text1 }{ text2 }$c={ text3 }$d={ text4 }


&layout(title="hello, world!", navsize=160, navleft=True)
&header={
$layout(title="hello, world!", navsize=160, navleft=True)
$header={
<h1>This is page header!</h1>
}&footer={
}$footer={
<small>All right reserved</small>
}&navbar={
}$navbar={
<ul>
<li>item1
<li>item2
</ul>
}&content={
}$content={

&unit{ text }
&columns("1/4", "1/4", "1/2") { text }{ text }{ text }
$unit{ text }
$columns("1/4", "1/4", "1/2") { text }{ text }{ text }

}
4 changes: 4 additions & 0 deletions pony/examples/templates/A.hello.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$layout("Greeting to " + self.name)
{
<h1>Hello, $self.name!</h1>
}
47 changes: 0 additions & 47 deletions pony/examples/templates/decorators.py

This file was deleted.

30 changes: 15 additions & 15 deletions pony/examples/templates/page1.template
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<html>
<head><title>&(self.title)</title></head>
<head><title>$self.title</title></head>
<body>
<h1>&(self.title)</h1>
<p>&(self.text)
&for(record in self.records) {
<h2>&(record.title)</h2>
&for(paragraph in record.paragraphs) {
<p>&(convert2html(paragraph))
<h1>$self.title</h1>
<p>$self.text
$for(record in self.records) {
<h2>$record.title</h2>
$for(paragraph in record.paragraphs) {
<p>$convert2html(paragraph)
}
&if(record.comments) {
$if(record.comments) {
<h3>comments:</h3>
<table>
&for(comment in record.comments) {
$for(comment in record.comments) {
<tr><td width="10%">
&(comment.date)<br>
&(comment.author)
$comment.date<br>
$comment.author
</td><td>
&(convert2html(comment.text))
$convert2html(comment.text)
</td></tr>
} &separator {
} $separator {
<tr><td colspan=2></td></tr>
}
</table>
} &// if
} &// for
} $// if
} $// for
<body>
</html>
19 changes: 19 additions & 0 deletions pony/examples/templates/script1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from pony.templating import html

def layout(title, content):
return html("""
<html>
<head><title>$title</title></head>
<body>
$content
</body></html>
""")

class A(object):
def __init__(self, name):
self.name = name
def hello(self):
return html()

a = A('John')
print a.hello()
47 changes: 47 additions & 0 deletions pony/examples/templates/script2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from pony.main import *

@http
@html
def index1(self):
print '<html><head>'
print '<title>%s</title>' % self.title
print '<link rel="stylesheet" type="text/css" href="%s">' % self.css
print '</head><body>'
print '<h1>%s</h1>' % self.greeting.upper()
print '<div id="main">'
print '<ul class="main-list">'
for text in self.list_content:
print '<li>%s</li>' % text
print '</ul></div>'
print '</body></html>'

@http
@html
def index2(self):
with tag.html:
with tag.head:
print tag.title(self.title)
print tag.link(rel="stylesheet", type="text/css", href=self.css)
with tag.body:
print tag.h1(self.greeting.upper())
with tag.div(id="main"):
with tag.ul(class_="main-list"):
for text in self.list_content: print tag.li(text)

@http
def index3(self):
return html("""
<html><head>
<title>$self.title</title>
<link rel="stylesheet" type="text/css" href="$self.css">
</head><body>
<h1>$self.greeting.upper()</h1>
<div id="main">
<ul class="main-list">
$(for text in self.list_content){<li>$text</li>}
</ul>
</div>
</body></html>
""")

start_http_server('localhost:8080')
52 changes: 30 additions & 22 deletions pony/templating.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sys, os.path, threading, inspect, re, weakref
import sys, os.path, threading, inspect, re, weakref, textwrap

from utils import is_ident, decorator

Expand Down Expand Up @@ -172,16 +172,18 @@ def joinstrings(list):
([{]) # open brace (group 1)
| ([}]) # close brace (group 2)
| &(?:
(&) # double & (group 3)
| [$](?:
([$]) # double $ (group 3)
| ( # comments (group 4)
//.*?(?:\n|\Z) # &// comment
| /\*.*?(?:\*/|\Z) # &/* comment */
//.*?(?:\n|\Z) # $// comment
| /\*.*?(?:\*/|\Z) # $/* comment */
)
| ( [A-Za-z_]\w*\s* # statement multi-part name (group 5)
(?:\.\s*[A-Za-z_]\w*\s*)*
)?
[({] # start of statement content
| ([(]) # start of $(expression) (group 5)
| ([{]) # start of ${markup} (group 6)
| ( [A-Za-z_]\w* # multi-part name (group 7)
(?:\s*\.\s*[A-Za-z_]\w*)*
)
(\s*[({])? # start of statement content (group 8)
)
""", re.VERBOSE)
Expand Down Expand Up @@ -212,14 +214,18 @@ def parse_markup(text, pos=0, nested=False):
raise ParseError("Unexpected symbol '}'", text, end)
brace_counter -= 1
result.append('}')
elif i == 3: # &&
result.append('&')
elif i == 4: # &/* comment */ or &// comment
elif i == 3: # $$
result.append('$')
elif i == 4: # $/* comment */ or $// comment
pass
else: # &command(
assert i in (5, None)
cmd_name = match.group(5)
command, end = parse_command(text,match.start(),end-1,cmd_name)
elif i in (5, 6): # $(expression) or ${i18n markup}
command, end = parse_command(text, start, end-1, None)
result.append(command)
elif i == 7: # $expression.path
result.append((start, end, None, match.group(7), None, None))
elif i == 8: # $function.call(...)
cmd_name = match.group(7)
command, end = parse_command(text, match.start(), end-1, cmd_name)
result.append(command)
pos = end

Expand All @@ -230,7 +236,7 @@ def parse_markup(text, pos=0, nested=False):
(;) # end of command (group 1)
| ([{]) # start of markup block (group 2)
| ( # keyword argument (group 3)
&
[$]
([A-Za-z_]\w*) # keyword name (group 4)
\s*=\s*[{]
)
Expand Down Expand Up @@ -272,8 +278,8 @@ def parse_command(text, start, pos, name):
| ([)]) # close parenthesis (group 2)
| ( # comments (group 3):
\#.*?(?:\n|\Z) # - Python-style comment inside expressions
| &//.*?(?:\n|\Z) # - &// comment
| &/\*.*?(?:\*/|\Z) # - &/* comment */
| [$]//.*?(?:\n|\Z) # - $// comment
| [$]/\*.*?(?:\*/|\Z) # - $/* comment */
)
| '(?:[^'\\]|\\.)*?' # 'string'
| "(?:[^"\\]|\\.)*?" # "string"
Expand Down Expand Up @@ -634,7 +640,7 @@ def get_template_name(frame):

def _template(str_cls,
text=None, filename=None, globals=None, locals=None,
source_encoding='ascii'):
source_encoding='ascii', keep_indent=False):
if text and filename:
raise TypeError("template() function cannot accept both "
"'text' and 'filename' parameters at the same time")
Expand All @@ -644,6 +650,8 @@ def _template(str_cls,
f = file(filename)
text = f.read()
f.close()
elif not keep_indent:
text = textwrap.dedent(text)
markup = template_cache.get(text)
if not markup:
tree = parse_markup(text)[0]
Expand All @@ -655,9 +663,9 @@ def _template(str_cls,
return markup.eval(globals, locals)

def template(text=None, filename=None, globals=None, locals=None,
source_encoding='ascii'):
source_encoding='ascii', keep_indent=False):
return _template(unicode, text, filename, globals, locals, source_encoding)

def html(text=None, filename=None, globals=None, locals=None,
source_encoding='ascii'):
source_encoding='ascii', keep_indent=False):
return _template(Html, text, filename, globals, locals, source_encoding)

0 comments on commit be305bd

Please sign in to comment.