[ZPT] dictionaries, has_key, and the test function

Willem Broekema willem@pastelhorn.com
Mon, 16 Dec 2002 00:03:59 +0100


Tim Lynch wrote:
> <span tal:condition="python:test(results.has_key('parents'), 1, 0)">
>    <span tal:define="parents python:results['parents']">
>        do stuff
>    </span>
> </span>

The following seems to be equivalent and quite elegant:


   <span tal:define="parents results/parents | nothing"
         tal:condition="parents">

     do stuff

   </span>


- It uses "|" to give an alternative value in case the first one
   fails (I probably got this trick from the Zope Book Advanced
   ZPT page).

- "object/x" will first try "object.x" (attribute), then
   "object[x]" (key lookup), so no "python:" necessary to access
   the key.

- As 'tal:define' is executed before 'tal:condition', no need
   for a subtag just for the logic.


Would be great if Evan could decide not to remove that tal:define ;)


- Willem