Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Template Toolkit 2 strange behaviour

by Aramis (Initiate)
on Nov 16, 2011 at 18:37 UTC ( #938436=perlquestion: print w/ replies, xml ) Need Help??
Aramis has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have a code part that behaves different from what I expected. sub_expand returns a list of hash references. If it returns more than one hash reference, no problem, the template produces the expected output. If the array contains just one hash reference (hash #2 is removed), the TT2 assumes that every pair of the hash #1 becomes other hash refs, so 5 different records exist. If sub_expand() returns \@expand_result (reference to @expand_result), then everything is OK even with one hash ref. Is there anybody to able to explain me the difference of these two cases?

Thanks in advance,

regards.

#!/usr/bin/perl -w use Template 2.22; sub sub_expand() { @expand_result = (); push(@expand_result, #hash #1 {'item_name' => "item_name #1", 'item_desc' => 'item description #1', 'item_size' => 4, 'item_kind' => 'item_kind #1', 'item_type' => 'item_type #1'}, #hash #2 {'item_name' => "item_name #2", 'item_desc' => 'item description #2', 'item_size' => 4, 'item_kind' => 'item_kind #2', 'item_type' => 'item_type #2'} ); return @expand_result; } my $tt = Template->new({ INCLUDE_PATH => "./", VARIABLES => { EXP => \&sub_expand, #macro OUTPUT_PATH => "./", ABSOLUTE => 1 } }) || die $tt->error, "\n"; $tt->process("template.txt", {}, "output.txt") || die $tt->error;
template.txt
[%- FOREACH c = EXP() %] [% loop.count %]/[% loop.size %] - its size is [% c.size %] [% c.item_name %] [% c.item_kind %] [%- END %]

Comment on Template Toolkit 2 strange behaviour
Select or Download Code
Re: Template Toolkit 2 strange behaviour
by Tanktalus (Canon) on Nov 16, 2011 at 22:48 UTC

    This is one of those cases where TT is like so many other modules - trying to be as smart as possible. It's trying to handle lists of hashes, key-value pairs (in a hash), and many other things at the same time. And, like those other modules (I'm thinking of XML::Simple here (*)), sometimes it needs a nudge to get things right in certain cases. In this case, you want to make sure that TT always treats EXP's return as a list, so you have to give it sufficient metadata to ensure that is the case. When you return a list of two or more, it can figure out that it's a list, so all is fine. When you return a single hash ref, it can't tell that it's to be treated as a list when looping over it with the FOREACH, so it doesn't. The metadata that is missing is that which tells TT that this is still a list, even though there's only one item there. The easiest way to do that is to return a reference to a list. Turns out that this is generally faster, too, as you are only copying one reference around instead of (potentially) multiple objects (references).

    My rule of thumb with TT is to always return array refs when I want my data treated as a list (looped in the template). I must have been bitten by this at some point, too. :-)

    (*) XML::Simple does this in the reverse - when parsing out an XML file, it only creates array refs when a second item is found with the same name at the same level, i.e., only to avoid collisions in the hash. Sometimes it's more convenient to always treat something as an array ref, so if that's what you want, you have to tell XML::Simple that.

      Thank you very much for your quick answer and great explanation.
Re: Template Toolkit 2 strange behaviour
by Rhandom (Curate) on Nov 17, 2011 at 17:42 UTC
    Template::Alloy, which should be a drop in replacement for Template::Tookit, has a configuration option called CALL_CONTEXT that can be set to one of smart (default), item, or list. There are many ways you can change the context you call in, you can set the global CALL_CONTEXT option during new, you can set it locally using a [% CONFIG CALL_CONTEXT => "list" %] inside your template, or you can use the @() and $() context specifiers (as in [% results = @( myobj.mymethod() ) %]).

    The following is a table of what is returned in each context copied from the Template::Alloy pod.

           return values      smart context   list context    item context
           -------------      -------------   ------------    ------------
        A   'foo'              'foo'           'foo'         'foo'
        B   undef              undef           undef         undef
        C   (no return value)  undef           []              undef
        D   (7)                7               7             7
        E   (7,8,9)            7,8,9         7,8,9         9
        F   @a = (7)           7               7             1
        G   @a = (7,8,9)       7,8,9         7,8,9         3
        H   ({b=>"c"})         {b=>"c"}        {b=>"c"}      {b=>"c"}
        I   (1)              1             [1]           1
        J   (1,2)          [1,2]       [1,2]       2
        K   7,8,9            7,8,9         [7,8,9]       7,8,9
        L   (undef, "foo")     die "foo"       undef, "foo"  "foo"
        M   wantarray?1:0      1               1             0
    

    Disclaimer: I am the author.

    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      Thanks for your recommendation, I had already checked the reviews and your module at CPAN. I will try it.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://938436]
Approved by Corion
Front-paged by Tanktalus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2014-09-01 15:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (14 votes), past polls