> Date: Wed, 20 Apr 2011 12:48:00 +0100
> From: Mike Raynham <[email protected]>
> Subject: Re: [Templates] zero values and "or"
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> On 20/04/11 12:15, Jacob Rask wrote:
> > Is there some way to use, or replace, OR with something that handles
> > values of 0? The following code:
> >
> > title = "0";
> >
> > title or 'untitled';
> >
> > will print 'untitled'.
> >
> > I could use:
> > IF title.defined; title; ELSE; 'untitled'; END;
> > or
> > IF title.length> 0; title; ELSE; 'untitled'; END;
> >
> > but the "or" operator is very useful. How would you do it?
> >
> > /Jacob
>
> As far as I am aware, TT doesn't have a defined-or operator, so instead,
> you could try the conditional (or ternary) operator:
>
> [% title = '0' %]
> [%# Prints <p>0</> %]
> <p>[% title.defined ? title : 'untitled' %]</p>
>
> [% title = '' %]
> [%# Prints <p></p> %]
> <p>[% title.defined ? title : 'untitled' %]</p>
>
> Alternatively, you could use 'length' instead of 'defined' to catch
> empty strings:
>
> [% title = '0' %]
> [%# Prints <p>0</> %]
> <p>[% title.length ? title : 'untitled' %]</p>
>
> [% title = '' %]
> [%# Prints <p>untitled</p> %]
> <p>[% title.length ? title : 'untitled' %]</p>
>
>
>
> Regards,
>
> Mike
Thanks, what I ended up using is:
[%
MACRO either(vars)
FOREACH var IN vars;
IF var.length;
var;
BREAK;
END;
END;
%]
[% either([title, 'untitled']) %]
It also allows any number of variables to test, just like OR.
/Jacob
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates