[Zope] Bag 'o newbie questions

Chris Withers chrisw@nipltd.com
Sat, 17 Aug 2002 18:44:51 +0100


Michael S. Fischer wrote:
> I really wanted to write something like this, but I couldn't get it
> to work:
> 
> <span tal:define="object python:test(hasattr(container.aq_explicit, 
>                                               'content_html'), 
>                                           content_html, toc_html)"
>       tal:replace="structure object">
> </span>
> 
> Apparently the test() function actually tries to evaluate the (possibly
> non-existing) content_html object even if the hasattr() returns 0.

yup, that's how python works :-S

You have to write it as:

(hasattr(container.aq_explicit, 'content_html') and container.content_html) or 
here.toc_html

..which causes python to execute somewhat differently.

That said, this is a bit more graceful:

getattr(container.aq_explicit,'content_html',here.toc_html)

However, you came up with quite a graceful solution, I'd code it as:

<tal:x replace="structure here/aq_explicit/default_html | here/toc_html"/>

cheers,

Chris