josh803316 has asked for the wisdom of the Perl Monks concerning the following question:

This should be an easy answer but I couldn't find it in the template toolkit docs. How can I check the variable type inside a template? What is the equivalent ref function or is there one? How is the following usually checked?
[% IF ref(variable) eq 'HASH' %] [% FOREACH pair in variable.pairs %] KEY: [% pair.key %] VALUE: [% pair.value %] [% END %] [% ELSIF ref(variable) eq 'ARRAY' %] [% FOREACH item in variable %] ITEM: [% item %] [% END %] [% ELSE %] [% variable %] [% END %]

Replies are listed 'Best First'.
Re: Checking the ref value inside a tt2 template
by Anonymous Monk on Dec 18, 2010 at 04:46 UTC
    See Template::Manual::VMethods, and create a custom virtual method called , isa, and use it like
    [% IF variable.isa('HASH') %] [% FOREACH pair in variable.pairs %] KEY: [% pair.key %] VALUE: [% pair.value %] [% END %] [% ELSIF variable.isa('ARRAY') %] [% FOREACH item in variable %] ITEM: [% item %] [% END %] [% ELSE %] [% variable %] [% END %]