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.


In reply to Re: Template Toolkit 2 strange behaviour by Tanktalus
in thread 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.