On Fri, 10 Sep 2010 17:30:03 -0400
Alan Evans <[email protected]> wrote:

> I have a hash that is being passed to TT like.
> 
> 'members' => [{ 'cn' =>  [ 'Some Name' ] }]
> 
> And I would like to sort by the member's cn's.
> 
> Any thoughts on how I would acomplish that?
> 
> members.sort('cn') would seem to work IF 'cn' wasn't another LIST.  I
> tried members.sort('cn.0') in the hope that TT was really really
> clever.  But no luck.

What I would do is make a custom vmethod in the calling perl script:

use Template ();
use Template::Stash ();

$Template::Stash::LIST_OPS->{'sortcn'} = sub {
        my $list = shift;
        @$list = sort { $a->{'cn'}->[0] cmp $b->{'cn'}->[0] } @$list;
        return $list;
};


And then in the template:  [% members.sortcn %]

Keep in mind, that will modify your original 'members' list.

Also, this is completely untested.  If it breaks, feel free to keep both
parts. :-)



_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to