[ZPT] how do I test the existence of a variable?

seb bacon seb@jamkit.com
21 Mar 2002 15:12:15 +0000


Thanks to everyone's replys, but we're still not there!

Harry's response comes closest to my requirements:

> > incidentally, the reason I need to do this is because I'm trying to
> > parameterise my macros, thus:
> >
> > 1. Macro definition:
> >
> > <p metal:define-macro="foo"
> >    tal:define="bar mynamespace/bar | python:[]">
> >  <span tal:repeat="item bar">
> >    stuff
> >  </span>
> > </p>
> >
> >
> > 2: Using it with a parameter:
> >
> > <div tal:define="bar python:['a','b','c']">
> >  <p metal:use-macro="foo">
> >    My macro goes here
> >  </p>
> > </div>
> 
> Maybe I've missed the point but this seems simple enough to me.  I've written 
> macros the way I think you're describing before just like this:
> 
> 1. Macro definition:
> 
> <p metal:define-macro="foo"
>    tal:define="rhubarb bar | python:[]">
>  <span tal:repeat="item rhubarb">
>    stuff
>  </span>
> </p>
> 
> 'bar' just gets plucked out the current namespace.  Or maybe I've missed some 
> deep and complicated problem here : )

Right, but the problem is that I want to be able to use the 'foo' macro
*even if there is no 'bar' in the current namespace*.

I'm doing it like this currently:

 ...<span tal:condition="exists:bar">
      do stuff
    </span>
    <span tal:condition="not:exists:bar">
      do other stuff
    </span>

What I'd like is to avoid that kind of code branching because I find it
unreadable in ZPT.  Instead I'd like to get either the value of bar if
it exists, or a default value if it doesn't, in an expression.

Perhaps I'm just asking too much :-)

seb