Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Template Toolkit 2 strange behaviour

by Tanktalus (Canon)
on Nov 16, 2011 at 22:48 UTC ( #938482=note: print w/ replies, xml ) Need Help??


in reply to Template Toolkit 2 strange behaviour

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.


Comment on Re: Template Toolkit 2 strange behaviour
Re^2: Template Toolkit 2 strange behaviour
by Aramis (Initiate) on Nov 17, 2011 at 18:56 UTC
    Thank you very much for your quick answer and great explanation.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://938482]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2014-08-23 14:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (174 votes), past polls