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 %]

In reply to Template Toolkit 2 strange behaviour by Aramis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.