[Templates] Lists and rand like http://faq.perl.org/perlfaq4.html#How_do_I_select_a_ra
Mark Mills
extremely@hostile.org
Tue, 18 Jul 2006 13:50:56 -0400
This is a multi-part message in MIME format.
--------------010805090405050600050401
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
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...)
>
> 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
--------------010805090405050600050401
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Perrin Harkins wrote:
<blockquote cite="mid1153243460.5327.17.camel@localhost.localdomain"
type="cite">
<pre wrap="">On Tue, 2006-07-18 at 18:02 +0100, Gavin Henry wrote:
</pre>
<blockquote type="cite">
<pre wrap="">What's the proper way ;-)
</pre>
</blockquote>
<pre wrap=""><!---->
Picking a random one before running the template and passing it in.
(You did ask...)
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
</pre>
</blockquote>
Hear, Hear for picking it in code rather than the template. :)<br>
<br>
But, the needy might also look at the ListUtil Plugin. It has a random
list item function.<br>
<a class="moz-txt-link-freetext" href="http://search.cpan.org/~markf/Template-Plugin-ListUtil-0.02/lib/Template/Plugin/ListUtil.pm">http://search.cpan.org/~markf/Template-Plugin-ListUtil-0.02/lib/Template/Plugin/ListUtil.pm</a><br>
<br>
Or make your own Stash OP for it:<br>
<br>
use Template::Stash;<br>
$Template::Stash::LIST_OPS->{ random } = sub { return $_[0][rand
@{$_[0]}] ;};<br>
<br>
--mark mills<br>
</body>
</html>
--------------010805090405050600050401--