The following code dies with a var.undef error because var is not defined. It works if I change the STRICT option to 0, but I'd like to keep strict checks, unless it's in an [% IF var %] check in the template. How do I do that?
use strict;
use warnings;
use Template;
my $t = Template->new(STRICT => 1) || die Template->error();
my $template = '[% IF var %][% var %][% END%]';
my $output = '';
$t->process(\$template, {}, \$output) || die $t->error(), "\n";
print "$output\n";
You are sending in an empty hash reference that doesn't contain the var variable that you're asking the template to display.
You can either check in your Perl code to set a sane default (in this example, I just hard-code it into the call to process()):
$t->process(\$template, {var => 55}, $output) || die $t->error(), "\n";
Output:
55
...or, you can tell the template to set its own sane default if the var variable isn't sent on the way in (ie, it's undefined):
my $template = '[% DEFAULT var = "sane" %][% IF var %][% var %][% END%]';
Output:
sane
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With