my
(
$output
,
$template
,
$result
);
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'var.tmpl'
);
$template
->param(
foo
=>
sub
{
'bar'
});
$output
= clean(
$template
->output);
is(
$output
,
'bar'
,
'correct value returned'
);
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'escapes.tmpl'
);
$template
->param(
foo
=>
sub
{
'<bar "foo">'
});
$output
= clean(
$template
->output);
is(
$output
,
'<bar "foo"> <bar "foo"> <bar "foo"> <bar \"foo\"> %3Cbar%20%22foo%22%3E'
,
'correct value escaped'
);
my
$count
= 0;
$template
->param(
foo
=>
sub
{ ++
$count
});
$output
= clean(
$template
->output);
is(
$output
,
'1 2 3 4 5'
,
'sub called multiple times'
);
is(
$count
, 5,
'correctly number of increments'
);
$count
= 0;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'escapes.tmpl'
,
cache_lazy_vars
=> 1);
$template
->param(
foo
=>
sub
{ ++
$count
});
$output
= clean(
$template
->output);
is(
$output
,
'1 1 1 1 1'
,
'cache_lazy_vars works'
);
is(
$count
, 1,
'correctly number of increments'
);
$output
= clean(
$template
->output);
is(
$output
,
'1 1 1 1 1'
,
'cache_lazy_vars works: output() called multiple times'
);
is(
$count
, 1,
'correctly number of increments'
);
$count
= 0;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'var.tmpl'
,
die_on_bad_params
=> 0);
$template
->param(
bar
=>
sub
{
$count
++ });
$output
= clean(
$template
->output);
is(
$output
,
''
,
'no output'
);
is(
$count
, 0,
'bar sub never called'
);
$count
= 0;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'if.tmpl'
);
$template
->param(
bool
=>
sub
{
$count
++ });
$output
= clean(
$template
->output);
is(
$output
,
'This is a line outside the if.'
,
'conditional false and then true'
);
is(
$count
, 2,
'currect number of increments'
);
$template
->param(
bool
=>
sub
{ --
$count
});
$output
= clean(
$template
->output);
is(
$output
,
'This is a line outside the if. INSIDE the if unless'
,
'conditional true and then false'
);
is(
$count
, 0,
'currect number of decrements'
);
$count
= 0;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'if.tmpl'
,
cache_lazy_vars
=> 1);
$template
->param(
bool
=>
sub
{
$count
++ });
$output
= clean(
$template
->output);
is(
$output
,
'This is a line outside the if. unless'
,
'conditional false w/cache_lazy_vars'
);
is(
$count
, 1,
'currect number of increments'
);
$count
= 1;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'if.tmpl'
,
cache_lazy_vars
=> 1);
$template
->param(
bool
=>
sub
{
$count
-- });
$output
= clean(
$template
->output);
is(
$output
,
'This is a line outside the if. INSIDE the if'
,
'conditional true w/cache_lazy_vars'
);
is(
$count
, 0,
'currect number of decrements'
);
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop.tmpl'
);
$template
->param(
loop_one
=>
sub
{ [{
var
=> 1}, {
var
=> 2}] });
$output
= clean(
$template
->output);
is(
$output
,
'1 2'
,
'simple coderef for a loop'
);
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop.tmpl'
,
cache_lazy_loops
=> 1);
$template
->param(
loop_one
=>
sub
{ [{
var
=> 1}, {
var
=> 2}] });
$output
= clean(
$template
->output);
is(
$output
,
'1 2'
,
'simple coderef for a loop (cache_lazy_loops doesnt change anything)'
);
$count
= 0;
$template
->param(
loop_one
=>
sub
{ [{
var
=>
$count
++}, {
var
=>
$count
++}] });
$output
= clean(
$template
->output);
is(
$output
,
'0 1'
,
'loop coderef that returns something different each time'
);
is(
$count
, 2,
'currect number of increments'
);
$count
= 0;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop.tmpl'
,
cache_lazy_loops
=> 1);
$template
->param(
loop_one
=>
sub
{ [{
var
=>
$count
++}, {
var
=>
$count
++}] });
$output
= clean(
$template
->output);
is(
$output
,
'0 1'
,
'loop coderef that returns something different each time, w/cache_lazy_loops'
);
is(
$count
, 2,
'currect number of increments'
);
$count
= 0;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop-if.tmpl'
);
$template
->param(
loop_one
=>
sub
{ [{
var
=>
$count
++}, {
var
=>
$count
++}] });
$output
= clean(
$template
->output);
is(
$output
,
'2 3'
,
'loop coderef that returns something different each time'
);
is(
$count
, 4,
'currect number of increments'
);
$count
= 1;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop-if.tmpl'
,
cache_lazy_loops
=> 1);
$template
->param(
loop_one
=>
sub
{ [{
var
=>
$count
++}, {
var
=>
$count
++}] });
$output
= clean(
$template
->output);
is(
$output
,
'1 2'
,
'loop coderef that returns something different each time w/cache_lazy_loops'
);
is(
$count
, 3,
'currect number of increments'
);
$count
= 1;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop-if.tmpl'
);
$template
->param(
loop_one
=>
sub
{
$count
-- ? [{
var
=> 1}, {
var
=> 2}] : [] });
$output
= clean(
$template
->output);
is(
$output
,
''
,
'no output because loop is blank the 2nd time'
);
is(
$count
, -1,
'currect number of decrements'
);
$count
= 1;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop-if.tmpl'
,
cache_lazy_loops
=> 1);
$template
->param(
loop_one
=>
sub
{
$count
-- ? [{
var
=> 1}, {
var
=> 2}] : [] });
$output
= clean(
$template
->output);
is(
$output
,
'1 2'
,
'loop isnt blank the 2nd time because of cache_lazy_loops'
);
is(
$count
, 0,
'currect number of decrements'
);
$count
= 1;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop-if.tmpl'
);
$template
->param(
loop_one
=>
sub
{
$count
-- ? [] : [{
var
=> 1}, {
var
=> 2}] });
$output
= clean(
$template
->output);
is(
$output
,
'Loop not filled in!'
,
'coderef returns empty loop the first time and doesnt get called the 2nd'
);
is(
$count
, 0,
'currect number of decrements'
);
$count
= 1;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop-if.tmpl'
,
cache_lazy_loops
=> 1);
$template
->param(
loop_one
=>
sub
{
$count
-- ? [] : [{
var
=> 1}, {
var
=> 2}] });
$output
= clean(
$template
->output);
is(
$output
,
'Loop not filled in!'
,
'coderef returns empty loop the first time and cache_lazy_loops caches it'
);
is(
$count
, 0,
'currect number of decrements'
);
$count
= 0;
$template
= HTML::Template->new(
path
=>
'templates'
,
filename
=>
'loop.tmpl'
,
die_on_bad_params
=> 0);
$template
->param(
loop_ten
=>
sub
{
$count
++; [{
var
=> 1}, {
var
=> 2}] });
$output
= clean(
$template
->output);
is(
$output
,
''
,
'no output because loop doesnt exist'
);
is(
$count
, 0,
'loop sub never called'
);
$template
= HTML::Template->new(
scalarref
=> \
'<tmpl_if foo><tmpl_loop bar>0</tmpl_loop></tmpl_if>'
,
die_on_bad_params
=> 0);
$template
->param(
bar
=>
sub
{
$count
++; [{
var
=> 1}, {
var
=> 2}] });
$output
= clean(
$template
->output);
is(
$output
,
''
,
'no output because surrounding conditional is false'
);
is(
$count
, 0,
'loop sub never called'
);
sub
clean {
my
$string
=
shift
;
$string
=~ s/\s+/ /g;
$string
=~ s/^\s+//;
$string
=~ s/\s+$//;
return
$string
;
}