Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Sort a hash of hashes by value in template toolkit

by Somni (Friar)
on Nov 16, 2007 at 15:15 UTC ( [id://651233]=note: print w/replies, xml ) Need Help??


in reply to Sort a hash of hashes by value in template toolkit

The sort (and nsort) methods are necessarily limited. You might be looking for the pairs method. See Template::Manual::VMethods.

If your sorting needs are at all complex you're going to have to pass it to some Perl to handle. Either a variable that's a subroutine, or a template plugin.

  • Comment on Re: Sort a hash of hashes by value in template toolkit

Replies are listed 'Best First'.
Re^2: Sort a hash of hashes by value in template toolkit
by dariusj (Sexton) on Nov 19, 2007 at 11:50 UTC
    Gave up on doing it in template toolkit, solved the issue by having a perl method in the handler that returns a sorted array of arrays which can then be easily looped in the .tt file.

    An ugly solution, but it works.

      I came across this issue as well. I was passing a hash of hashes to TT and used this method to sort it.
      Sample Hash:
      %Orders = {}; $Orders{1}{NAME} = 'Bill'; $Orders{1}{STATUS} = 'Complete'; $Orders{2}{NAME} = 'John'; $Orders{3}{STATUS} = 'Pending';

      Sort the hash by key value (orderid) descending and output data from the nested hashes:

      <table> <tr> <th>Order ID</th> <th>Name</th> <th>Status</th> </tr> [% sortedKEYS = Orders.nsort %] [% FOREACH skey IN sortedKEYS %] <tr> <td>[% skey %]</td> <td>[% Orders.$skey.NAME %]</td> <td>[% Orders.$skey.STATUS %]</td> </tr> [% END %] </table>

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (1)
As of 2024-04-25 03:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found