[ZPT] testing for the existence of a variable

SMITH, KEVIN J. kevin.j.smith@certen.ca
29 Nov 2002 13:09:04 -0500


Many, many thanks - I have spent many hours trying millions of different
ways of doing this to no avail.  I love the concept of Zope and ZPT but
I find the paradigm shift a bit onerous compared to PHP or ASP or Perl
(even with an object oriented background)

However, I'm sticking with it in the hopes it will pay big dividends!!

Cheers

On Thu, 2002-11-28 at 17:33, Clemens Robbenhaar wrote:
> 
> 
>  > 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
>