[Zope-CMF] Re: TAL: Semantics of the | operator inside tal:condition

Andreas Jung andreas at andreas-jung.com
Mon Jan 19 01:10:53 EST 2004


Thanks to Tres for the clarification. I think this was necessary because
several developers saw the code below  and they were convinced  of the
'or' semantics.

Andreas

--On Sonntag, 18. Januar 2004 16:41 Uhr -0500 Tres Seaver 
<tseaver at zope.com> wrote:

> Andreas Jung wrote:
>> While debugging a problem in Plone 2 I came across some code that
>> uses tal:condition="expr1|expr2"  that was never executed  if expr1
>> evaluated to False. Here is a small example that show the problem
>>
>> <div tal:define="a python: []; b python: [1,2,3]">
>>  <span tal:condition="a|b">
>>    hello world
>>  </span>
>> </div>
>>
>> Running the template outputs nothing instead of 'hello world'
>>
>> Is there a reasonable explanation for this?
>>
>> Also look at this code here:
>>
>> <div tal:define="a python: None; b python: 42; c a|b">
>> <span tal:content="c" />
>> </div>
>>
>> One would expect that 'c' is assigned to 42. At least this was the
>> opinion of some qualified plone developers. The TAL documentation is not
>> completely clear at this point. I know that the code can be rewritten
>> using test() but I would like to know about the semantics of the |
>> operator in this  case.
>
> See the "Alternate Paths" section of the "Advanced ZPT chapter of the
> Zope Boot",
>    http://zope.org/Documentation/Books/ZopeBook/2_6Edition/AdvZPT.stx
>
> The '|' operator in path expressions is not a "logical or";  it is a
> "fallback or".  The second clause is only tested if the first raises one
> of a set of exceptions (NameError, AttributeError, TypeError, IndexError,
> I think;  check the source to be sure);  such exceptions indicate that
> the first clause couldn't be evaluated.
>
> You want to use a Python expression to get "logical or" semantics".
>
> Tres.







More information about the Zope-CMF mailing list