Hi, I have the followong TAL-expression: <td tal:content="structure python:here.test(myImportantCondition and myExpensiveMethod(), myExpensiveMethod(), 'myDefaultString')">myDefaultString</td> To execute 'myExpensiveMethod()' 'myImportantCondition' must be 'true'. But I want to avoid to execute 'myExpensiveMethod()' two times. How can I achive that? As default-value myDefaultString should be served. I don't know a way with tal:condition 'cause on 'false' there is no way to get 'myDefaultString', isn't it? -- many thanks for your replies, Elena
Elena Schulz a écrit:
Hi,
I have the followong TAL-expression:
<td tal:content="structure python:here.test(myImportantCondition and myExpensiveMethod(), myExpensiveMethod(), 'myDefaultString')">myDefaultString</td>
use python:test(<your test>) instead of python:here.test(<your test>) --- -------
To execute 'myExpensiveMethod()' 'myImportantCondition' must be 'true'. But I want to avoid to execute 'myExpensiveMethod()' two times. How can I achive that? As default-value myDefaultString should be served.
I don't know a way with tal:condition 'cause on 'false' there is no way to get 'myDefaultString', isn't it?
tal:condition="not:(here/myImportantCondition() and here/myExpensiveMethod()" tal:define method here/myExpensiveMethod() Due to my lack of experience, i'm not sure that the use of the parenthesis in myImportantCondition() and ...Method() is correct. Have a look at Page templates and TALES expressions in the zope book http://www.zope.org/Documentation/Books/ZopeBook/2_6Edition HTH
-- many thanks for your replies, Elena
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
-- Andre PZP Enthusiast
On Fri, Jan 31, 2003 at 12:12:55PM +0100, Elena Schulz wrote:
Hi,
I have the followong TAL-expression:
<td tal:content="structure python:here.test(myImportantCondition and myExpensiveMethod(), myExpensiveMethod(), 'myDefaultString')">myDefaultString</td>
To execute 'myExpensiveMethod()' 'myImportantCondition' must be 'true'. But I want to avoid to execute 'myExpensiveMethod()' two times. How can I achive that? As default-value myDefaultString should be served.
I've given this some thought, and it's actually rather difficult (if you want to invoke myExpensiveMethod() just once and only if myImportantCondition is true). Also most people don't seem to realize that test() doesn't do lazy evaluation - the ZPT above will *always* invoke at least once, no matter what myImportantCondition is. My advice would be to use a pythonscript. Cheers Ivo -- Drs. I.R. van der Wijk -=- Brouwersgracht 132 Amaze Internet Services V.O.F. 1013 HA Amsterdam, NL -=- T +31-20-4688336 F +31-20-4688337 Linux/Web/Zope/SQL/MMBase W http://www.amaze.nl E info@amaze.nl Network Solutions W http://vanderwijk.info E ivo@amaze.nl Consultancy PGP http://vanderwijk.info/pgp -=-
At 06:33 AM 1/31/2003, Ivo van der Wijk wrote:
My advice would be to use a pythonscript.
Ditto to that... it's trivial in Python: if myImportantCondition: return myExpensiveMethod or myDefaultValue return myDefaultValue Where those tokens are expanded into something meaningful, of course... :-) Dylan
Hi Dylan, Ivo, Max and Andre, I was in the hope that there would be an elegant zpt-way of doing this. So it seems that I have to do it with python. Hmm, slowly I get quite a bunch of those tiny little pythons snailing around. Maybe it should be done generally as a specific test-function for all decisions of this lazy-evaluation-kind. Its a pity that there is no easy '|' construct like in path-expressions. -- thanks for your answers, Elena
At 06:33 AM 1/31/2003, Ivo van der Wijk wrote:
My advice would be to use a pythonscript.
Ditto to that... it's trivial in Python:
if myImportantCondition: return myExpensiveMethod or myDefaultValue return myDefaultValue
Where those tokens are expanded into something meaningful, of course... :-)
Dylan
Elena Schulz wrote:
<td tal:content="structure python:here.test(myImportantCondition and myExpensiveMethod(), myExpensiveMethod(), 'myDefaultString')">myDefaultString</td>
To execute 'myExpensiveMethod()' 'myImportantCondition' must be 'true'. But I want to avoid to execute 'myExpensiveMethod()' two times. How can I achive that? As default-value myDefaultString should be served.
Thanks to the way Python's "and" works, you can do the following: <td tal:define="x python:myImportantCondition and myExpensiveMethod()" tal:content="python:x or default">myDefaultString</td> If myImportantCondition is false, myExpensiveMethod will not be evaluated, x will be false, and 'x or default' will evaluate to 'default', which leaves your 'myDefaultString' untouched. If myImportantCondition is true, myExpensiveMethod will be evaluated and the result placed in x. Then 'x or default' will evaluate to 'default' if myExpensiveMethod() was false, or whatever value it returned otherwise. Cheers, Evan @ 4-am
Hi Evan, BINGO, that's it --- just when I was getting familar with the idea of adopting one more cute python-thingy your idea came along. I knew it should be possible :), but the idea of putting the condition in the define-slot is just exellent and makes it. My zen of python has increased one step further, thanks a lot. May this one should be mentioned at zope-labs. (you get a 5 star-rating from my side) -- thanks again, Elena
Thanks to the way Python's "and" works, you can do the following:
<td tal:define="x python:myImportantCondition and myExpensiveMethod()" tal:content="python:x or default">myDefaultString</td>
On Fri, Jan 31, 2003 at 10:12:14AM -0600, Evan Simpson wrote:
Thanks to the way Python's "and" works, you can do the following:
<td tal:define="x python:myImportantCondition and myExpensiveMethod()" tal:content="python:x or default">myDefaultString</td>
Yes, i use this idiom a lot, abbreviated to: <td tal:content="python:myImportantCondition and myExpensiveMethod() or default"> my default string </td> However, be wary of this: the inevitable need to add "just one more option" can lead to some REALLY ugly expressions. You don't want to end up with expressions like: <span tal:content="python:here.foo(bar) and here.that and not (request.the_other) or not container.spam or here.get_some_food(request.food_type or 'bacon')"> what on earth is going to end up here? </span> as a rule of thumb, I like to refactor anything that uses more than 2 of "and" or "or". -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's POLY BUTTERY KATANA CAKE! (random hero from isometric.spaceninja.com)
Elena Schulz wrote at 2003-1-31 12:12 +0100:
I have the followong TAL-expression:
<td tal:content="structure python:here.test(myImportantCondition and myExpensiveMethod(), myExpensiveMethod(), 'myDefaultString')">myDefaultString</td> The safe way looks like:
<... tal:define="myRes python:myCond and myMethod()" tal:content="python:test(myCond,myRes,default)" ... When you know that "myMethod()" cannot return a Python false value, you can use ... tal:content="python myCond and myMethod() or default" ... Dieter
On Fri, Jan 31, 2003 at 09:29:47PM +0100, Dieter Maurer wrote:
When you know that "myMethod()" cannot return a Python false value, you can use
... tal:content="python myCond and myMethod() or default"
hmm, ok, I see the difference. In this expression, you might consider "" to be valid output from myMethod(), or not. It's a problem if you do. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's THE LECTRO ACTUATOR! (random hero from isometric.spaceninja.com)
participants (7)
-
D2 -
Dieter Maurer -
Dylan Reinhardt -
Elena Schulz -
Evan Simpson -
Ivo van der Wijk -
Paul Winkler