This is a question about Template Toolkit for perl.
I render my templates with a little command-line utility that has the following option enabled
DEBUG => Template::Constants::DEBUG_UNDEF,
The syntax is render <file.tt> var1 val1 var2 val2 ....
This is very convenient because the user gets prompts about values that need to be defined, for example
$ render file.tt
undef error - var1 is undefined
$ render file.tt var1 foo
undef error - var2 is undefined
$ render file.tt var1 foo var2 bar
... template renders correctly
For some (optional) values, templates provide defaults, for example:
[%- DEFAULT
hostname = 0
%]
Then the template body would typically contain:
[% IF hostname %] hostname = [% hostname %][% ELSE %][% -- a comment, variable hostname not provided %][% END %]
How do I make the above idiom work for variables where 0
is a valid value?
I want the following to happen:
render template.tt
Template renders:
-- this is a comment, variable enable_networking not provided
For
render template.tt enable_networking 0
I want
enable_networking = 0
The problem is differentiating between defined values and false values. I have tried using -1
(instead of 0
) both in the DEFAULT
block and in the [% IF enable_networking == -1 %]
statement.
However, the following DEFAULT
block
[% DEFAULT enable_networking = -1 %]
will override value 0
specified on the command-line. (It sees a enable_networking
is false and sets it to -1)
Are there any easy work-arounds (some config variable maybe?)