[Radiant] any way to dynamically populate <r:random>?

Andrew Neil andrew.jr.neil at googlemail.com
Sat Jun 7 17:49:25 CDT 2008


On 6 Jun 2008, at 01:01, john muhl wrote:

> I'd like to insert part of a random subpage into the sidebar and  
> figured it'd be a good fit for the <r:random> tag but so far am  
> having very little luck. I've tried:
>
> <r:random>
>  <r:children:each>
>    <r:option><r:title/></r:option>
>  </r:children:each>
> </r:random>

I had a go at this too, but couldn't get it to work either. I had a  
look at the code and the specs (both pasted below), but couldn't  
figure out why this wouldn't work. It seems to me like this is a  
perfect example of where the <r:random> tag would be useful, but the  
spec doesn't currently test such a scenario.

I thought of an alternative approach which might be easier to  
implement. Here is an example:

<r:find url="/quotations">
   <r:children:each limit="1" order="rand">
       <r:content part="body"/>
   </r:children:each>
</r:find>

This doesn't work at the moment, because the order attribute can only  
be asc or desc. I think it would be pretty easy to add the option for  
random order though.

Cheers,
Drew

-----------------------------------------------------------------------------------

app//models/standard_tags.rb
   desc %{
     Randomly renders one of the options specified by the @option@ tags.

     *Usage:*
     <pre><code><r:random>
       <r:option>...</r:option>
       <r:option>...</r:option>
       ...
     <r:random>
     </code></pre>
   }
   tag 'random' do |tag|
     tag.locals.random = []
     tag.expand
     options = tag.locals.random
     option = options[rand(options.size)]
     option.call if option
   end
   tag 'random:option' do |tag|
     items = tag.locals.random
     items << tag.block
   end

-----------------------------------------------------------------------------------
spec/models/standard_tags_spec.rb
   it '<r:random> should render a randomly selected contained  
<r:option>' do
     page.should render("<r:random> <r:option>1</r:option>  
<r:option>2</r:option> <r:option>3</r:option> </r:random>").matching(/ 
^(1|2|3)$/)
   end


More information about the Radiant mailing list