[ZPT] testing for the existence of a variable

Clemens Robbenhaar zpt@zope.org
Thu, 28 Nov 2002 23:33:11 +0100


 > I want to set the value of a variable in ZPT only if it isn't already
 > set.  The problem is whenever I reference the variable when it has not
 > been set I get an error.  This sounds so simple but I have tried many
 > different ways to accomplish this and every time I come up empty handed.
 > 
 > example:  this is what I want to do ...
 > 
 > <span tal:define="language python:test(language, language,
 > 'french')></span>
 > 
 > or in a Perl paradigm:
 > 
 > unless ($language) { $language = "french"; }
 > 
 > Does anybody have an elegant solution to this problem?

there is a simple trick I have borrowed from page templates in the Silva
product (most probably in use a lot elsewhere, too).

 tal:define="language language | string:french"

may do what You want. However it sets the variable if it is defined and
the empty string, too, thus You may prefer:

 tal:define="language exists:language | string:french"

Cheers,
Clemens