[Zope] Re: TAL: test() or condition Howto?
Evan Simpson
evan@4-am.com
Fri, 31 Jan 2003 10:12:14 -0600
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