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.
template.txt#!/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;
[%- FOREACH c = EXP() %] [% loop.count %]/[% loop.size %] - its size is [% c.size %] [% c.item_name %] [% c.item_kind %] [%- END %]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Template Toolkit 2 strange behaviour
by Tanktalus (Canon) on Nov 16, 2011 at 22:48 UTC | |
by Aramis (Initiate) on Nov 17, 2011 at 18:56 UTC | |
|
Re: Template Toolkit 2 strange behaviour
by Rhandom (Curate) on Nov 17, 2011 at 17:42 UTC | |
by Aramis (Initiate) on Nov 17, 2011 at 18:59 UTC |