Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-19 03:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found