[Templates] Lists and rand
like http://faq.perl.org/perlfaq4.html#How_do_I_select_a_ra
Gavin Henry
ghenry@perl.me.uk
Tue, 18 Jul 2006 19:52:14 +0100 (BST)
<quote who="Mark Mills">
> Perrin Harkins wrote:
>> On Tue, 2006-07-18 at 18:02 +0100, Gavin Henry wrote:
>>
>>> What's the proper way ;-)
>>>
>>
>> Picking a random one before running the template and passing it in.
>> (You did ask...)
Well, this is for Catalyst, so it's classed as View logic, so I kept it in
the template.
>>
>> However, if you just make a sub that does what you want with the list
>> ref, you can pass a reference to it to TT in the stash and run it:
>>
>> [ % my_rand_func(tips) %]
>>
>> - Perrin
>>
> Hear, Hear for picking it in code rather than the template. :)
>
> But, the needy might also look at the ListUtil Plugin. It has a random
> list item function.
>
> http://search.cpan.org/~markf/Template-Plugin-ListUtil-0.02/lib/Template/Plugin/ListUtil.pm
>
> Or make your own Stash OP for it:
>
> use Template::Stash;
> $Template::Stash::LIST_OPS->{ random } = sub { return $_[0][rand
> @{$_[0]}] ;};
>
> --mark mills
>