use_ok(
'HTML::Template'
);
my
$tmpl_string
=
""
;
my
$template
= HTML::Template->new_scalar_ref(\
$tmpl_string
);
eval
{
my
$value
=
$template
->param(
'FOOBAR'
) };
like(
$@,
qr/HTML::Template : Attempt to get nonexistent parameter/
,
"attempt to get nonexistent parameter caught with die_on_bad_params == 1"
);
$template
= HTML::Template->new_scalar_ref(\
$tmpl_string
,
die_on_bad_params
=> 0);
ok(!
defined
(
$template
->param(
'FOOBAR'
)),
"if bad params permitted, bad param results in undef"
);
$template
->param(
'FOOBAR'
=>
undef
);
ok(!
defined
(
$template
->param(
'FOOBAR'
)),
"undef param results in undef"
);
eval
{
my
$value
=
$template
->param(
bless
[
'FOOBAR'
],
"Puppy"
) };
like($@,
qr/Single reference arg to param\(\) must be a hash-ref/
,
"Single reference arg to param() must be a hash-ref!"
);
eval
{
$template
->param(
bless
{
'FOOBAR'
=> 42},
"Puppy"
) };
ok(!$@,
"param() doesn't die with blessed hash as first arg"
);
eval
{
$template
->param(\{}) };
like($@,
qr/must be a hash-ref/
i,
'reference to a hash-ref is still bad'
);
eval
{
my
$value
=
$template
->param(
'foo'
=> 1,
'bar'
) };
like($@,
qr/You gave me an odd number of parameters to param/
,
"odd number of args to param"
);
$template
= HTML::Template->new_scalar_ref(\
'<tmpl_var foo>'
);
eval
{
$template
->param(
foo
=> \{}) };
like($@,
qr/a reference to a reference/
i,
'trying to use a reference to a reference'
);
$template
= HTML::Template->new_scalar_ref(\
'<tmpl_var foo> <tmpl_var bar> <tmpl_var baz> <tmpl_loop frob><tmpl_var fooey>-<tmpl_var blah> </tmpl_loop>'
);
$template
->param(
foo
=> 1,
baz
=> 2);
$template
->param(
bar
=> 2,
baz
=> 3,
frob
=> [{
fooey
=>
'a'
,
blah
=>
'b'
}, {
fooey
=>
'c'
,
blah
=>
'd'
}]);
is(
$template
->output,
'1 2 3 a-b c-d '
,
'can set multiple params at once'
);