[Zope] Re: TAL: test() or condition Howto?
Paul Winkler
pw_lists@slinkp.com
Fri, 31 Jan 2003 09:35:56 -0800
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)