Hi,

I have been using TT to conditionally show content based on 0/1
(true/false) values of variables.  In my template, if I set a default
to 1 (true/on) and I want to turn it off, passing a variable value of
0 will have no effect since the DEFAULT directive will ignore the 0
per the docs.

TT MANUAL>>The DEFAULT directive is similar to SET but only updates
TT MANUAL>>variables that are currently undefined or have no "true"
TT MANUAL>>value (in the Perl sense).

This leaves no distinction between 0 and undef meaning you can never
turn off a default by number, you would always have to use literal
strings like on/off.  Although the docs defer to Perl, is this the
desired result or should the DEFAULT directive allow the value of zero?

Here is a small script showing the issue:

use warnings;
use strict;

use Template;
my $template = Template->new;
$template->process(\*DATA) or die $template->error();
print "Done\n";
__DATA__

This test sets the default to 1 and tries to set variable value to 0
[% INCLUDE default_one_to_zero
            default_one_to_zero_var = 0 %]
This test sets the default to 0 and tries to set variable value to 1
[% INCLUDE  default_zero_to_one 
            default_zero_to_one_var = 1 %]

[%- BLOCK default_one_to_zero -%]
   [% DEFAULT
        default_one_to_zero_var = '1' -%]
   [% IF default_one_to_zero_var == 0 %]OK[% ELSE %]NOT OK[% END %]: variable = [% 
default_one_to_zero_var %] should be 0
[% END %]

[%- BLOCK default_zero_to_one -%]
   [% DEFAULT
        default_zero_to_one_var = 0 -%]
   [% IF default_zero_to_one_var == 1 %]OK[% ELSE %]NOT OK[% END %]: variable = [% 
default_zero_to_one_var %] should be 1
[% END %]


-- 
Best regards,
 christopher                          mailto:[EMAIL PROTECTED]


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to