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