[Checkins] SVN: z3c.pt/trunk/z3c/pt/ Fixed implementation of
tal:omit-tag.
Malthe Borch
mborch at gmail.com
Sat Dec 22 19:58:45 EST 2007
Log message for revision 82407:
Fixed implementation of tal:omit-tag.
Changed:
U z3c.pt/trunk/z3c/pt/clauses.py
U z3c.pt/trunk/z3c/pt/translation.py
U z3c.pt/trunk/z3c/pt/translation.txt
-=-
Modified: z3c.pt/trunk/z3c/pt/clauses.py
===================================================================
--- z3c.pt/trunk/z3c/pt/clauses.py 2007-12-23 00:56:49 UTC (rev 82406)
+++ z3c.pt/trunk/z3c/pt/clauses.py 2007-12-23 00:58:44 UTC (rev 82407)
@@ -223,7 +223,7 @@
Hello
World!
- Limited scope:
+ Finalized limited scope:
>>> stream = CodeIO()
>>> from StringIO import StringIO
@@ -237,11 +237,29 @@
>>> exec stream.getvalue()
>>> _out.getvalue()
'Hello'
+
+ Open limited scope:
+
+ >>> stream = CodeIO()
+ >>> from StringIO import StringIO
+ >>> _out = StringIO()
+ >>> true = Condition(expression("True"), [Tag('div')], finalize=False)
+ >>> false = Condition(expression("False"), [Tag('span')], finalize=False)
+ >>> true.begin(stream)
+ >>> stream.out("Hello World!")
+ >>> true.end(stream)
+ >>> false.begin(stream)
+ >>> false.end(stream)
+ >>> exec stream.getvalue()
+ >>> _out.getvalue()
+ '<div>Hello World!</div>'
+
"""
- def __init__(self, expression, clauses=None):
+ def __init__(self, expression, clauses=None, finalize=True):
self.assign = Assign(expression)
self.clauses = clauses
+ self.finalize = finalize
def begin(self, stream):
temp = stream.save()
@@ -251,15 +269,24 @@
if self.clauses:
for clause in self.clauses:
clause.begin(stream)
- for clause in reversed(self.clauses):
- clause.end(stream)
+ if self.finalize:
+ for clause in reversed(self.clauses):
+ clause.end(stream)
stream.outdent()
def end(self, stream):
- if not self.clauses:
+ temp = stream.restore()
+
+ if self.clauses:
+ if not self.finalize:
+ stream.write("if %s:" % temp)
+ stream.indent()
+ for clause in reversed(self.clauses):
+ clause.end(stream)
+ stream.outdent()
+ else:
stream.outdent()
self.assign.end(stream)
- stream.restore()
class Else(object):
def __init__(self, clauses=None):
@@ -316,7 +343,7 @@
"""
- def __init__(self, tag, attributes, selfclosing=False):
+ def __init__(self, tag, attributes={}, selfclosing=False):
i = tag.find('}')
if i != -1:
self.tag = tag[i+1:]
@@ -332,9 +359,13 @@
# attributes
for attribute, expression in self.attributes.items():
stream.out(' %s="' % attribute)
- write = Write(expression)
- write.begin(stream)
- write.end(stream)
+
+ if isinstance(expression, (tuple, list)):
+ write = Write(expression)
+ write.begin(stream)
+ write.end(stream)
+ else:
+ stream.out(expression.replace("'", "\\'"))
stream.out('"')
if self.selfclosing:
Modified: z3c.pt/trunk/z3c/pt/translation.py
===================================================================
--- z3c.pt/trunk/z3c/pt/translation.py 2007-12-23 00:56:49 UTC (rev 82406)
+++ z3c.pt/trunk/z3c/pt/translation.py 2007-12-23 00:58:44 UTC (rev 82407)
@@ -84,8 +84,10 @@
selfclosing = self.text is None and not dynamic
tag = clauses.Tag(self.tag, self._attributes(), selfclosing=selfclosing)
- if self.omit is not None:
- _.append(clauses.Condition(_not(self.omit), [tag]))
+ if self.omit:
+ _.append(clauses.Condition(_not(self.omit), [tag], finalize=False))
+ elif self.omit is not None:
+ pass
else:
_.append(tag)
@@ -188,7 +190,7 @@
# static attributes are at the bottom of the food chain
static = [key for key in self.keys() if not key.startswith('{')]
for key in static:
- attributes[key] = [repr(self.attrib[key])]
+ attributes[key] = self.attrib[key]
# dynamic attributes
attrs = self.attributes
Modified: z3c.pt/trunk/z3c/pt/translation.txt
===================================================================
--- z3c.pt/trunk/z3c/pt/translation.txt 2007-12-23 00:56:49 UTC (rev 82406)
+++ z3c.pt/trunk/z3c/pt/translation.txt 2007-12-23 00:58:44 UTC (rev 82407)
@@ -21,19 +21,23 @@
... <span id="test"
... class="dummy"
... tal:define="a 'abc'"
- ... tal:attributes="class 'def' + a"
+ ... tal:attributes="class 'def' + a; style 'position: absolute'"
... tal:content="a + 'ghi'" />
... <ul>
... <li tal:repeat="i range(5)">
... <span tal:replace="'Item ' + str(i) + ')'" />
... </li>
... </ul>
+ ... <p tal:omit-tag="">No paragraph here.</p>
+ ... <p tal:omit-tag="True">No paragraph here either.</p>
+ ... <p tal:omit-tag="False">A paragraph here.</p>
+ ... <span tal:replace="'Hello World!'">Hello Universe!</span>
... </div>
... """
>>> print render(body)
<div>
- <span id="test" class="defabc">abcghi</span>
+ <span style="position: absolute" id="test" class="defabc">abcghi</span>
<ul>
<li>
Item 0)
@@ -51,6 +55,10 @@
Item 4)
</li>
</ul>
+ No paragraph here.
+ No paragraph here either.
+ <p>A paragraph here.</p>
+ Hello World!
</div>
Error handling
@@ -86,5 +94,3 @@
>>> print render(body)
<div content="'Hello World'" />
-
-
More information about the Checkins
mailing list