# Problems with \<r:random\> tags

**URL:** <https://www.ruby-forum.com/t/problems-with-r-random-tags/66048>\
**Category:** Radiant CMS\
**Created:** [August 23, 2006, 1:04am UTC](https://www.ruby-forum.com/t/problems-with-r-random-tags/66048 "2006-08-23T01:04:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 1:04am UTC](https://www.ruby-forum.com/t/problems-with-r-random-tags/66048/1 "2006-08-23T01:04:11Z")

</div>

Hi,

I’m now having a problem with the \<r:random\> tag for inserting a  
random image into a snippet.

I’ve looked at  
[http://dev.radiantcms.org/radiant/browser/trunk/radiant/app/models/page\_context.rb](http://dev.radiantcms.org/radiant/browser/trunk/radiant/app/models/page_context.rb)  
where I see:

# \<r:random\>

# \<r:option\>…\</r:option\>

# \<r:option\>…\</r:option\>

# …

# \<r:random\>

# 

# Randomly renders one of the options specified by the ‘option’ tags.

# 

define\_tag ‘random’ do |tag|  
tag.locals.random = []  
tag.expand  
options = tag.locals.random  
option = options[rand(options.size)]  
option.call if option  
end  
define\_tag ‘random:option’ do |tag|  
items = tag.locals.random  
items \<\< tag.block  
end

It seems to me that the \<r:option\> tags used in the comments don’t  
match the line

define\_tag ‘random:option’ do |tag|

and so I tried \<r:random:option\> instead, but to no avail.

Here is the snippet:

 ![This is a random image](https://www.ruby-forum.com/images/banners/%20<r:random>%20<r:random:option>banner1</r:random:option>%20<r:random:option>banner2</r:random:option>%20</r:random>%20.jpg)

TIA,

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [August 23, 2006, 1:58am UTC](https://www.ruby-forum.com/t/problems-with-r-random-tags/66048/2 "2006-08-23T01:58:14Z")

</div>

Dave C. wrote:

> 

What output do you get from the above? Keep in mind that if the page is  
being cached you won’t see a difference for at least 5 minutes.

–  
John L.  
[http://wiseheartdesign.com](http://wiseheartdesign.com)

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/problems-with-r-random-tags/66048/3 "2006-09-04T14:46:07Z")

</div>

Hi John!

On 23/08/06, John W. Long [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > .jpg" /\>
> 
> What output do you get from the above? Keep in mind that if the page is  
> being cached you won’t see a difference for at least 5 minutes.

The output is:

🙂

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/problems-with-r-random-tags/66048/4 "2006-09-04T14:46:08Z")

</div>

On 23/08/06, John W. Long [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Dave C. wrote:
> 
> Ok, the extra new lines are the problem. Radiant tags observe whitespace  
> (that is, they won’t trim extra newlines off for you). Instead of the  
> what you had, try this:  
> \<r:snip\>  
> See the difference?

Yes - excellent - many thanks! 🙂

> Oh, and you don’t have to prefix the \<r:option /\>  
> tags with “random:”.

Yes I see this is how the documentation in the comments is; since it  
wasn’t working I tried looking at the ruby.

---

<div class="post-metadata">

**Author:** ![pezdude](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a87d85/32.png) [@pezdude](https://www.ruby-forum.com/u/pezdude)\
**Post date:** [September 4, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/problems-with-r-random-tags/66048/5 "2006-09-04T14:46:07Z")

</div>

Dave C. wrote:

> The output is:
> 
> ![iMethod](https://www.ruby-forum.com/images/banners/%20<p>.jpg"%20/></a></p>%20</div>%20<p>And%20yes%20I%20have%20cleared%20the%20page%20cache.</p>%20</blockquote>%20<p>Ok,%20the%20extra%20new%20lines%20are%20the%20problem.%20Radiant%20tags%20observe%20whitespace<br>%20(that%20is,%20they%20won%E2%80%99t%20trim%20extra%20newlines%20off%20for%20you).%20Instead%20of%20the<br>%20what%20you%20had,%20try%20this:</p>%20%20%20%20<div%20class=) ![This is a random image](https://www.ruby-forum.com/images/banners/<r:random>%20%20%20%20<r:option>banner1</r:option>%20%20%20%20<r:option>banner2</r:option>%20%20%20%20</r:random>.jpg)
> 
> This should output:
> 
> ![This is a random image](https://www.ruby-forum.com/images/banners/banner1.jpg)
> 
> See the difference? Oh, and you don’t have to prefix the \<r:option /\>  
> tags with “random:”.
> 
> –  
> John L.  
> [http://wiseheartdesign.com](http://wiseheartdesign.com)
