# \<r:random\> and page children

**URL:** <https://www.ruby-forum.com/t/r-random-and-page-children/68577>\
**Category:** Radiant CMS\
**Created:** [September 13, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577 "2006-09-13T18:57:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 13, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/1 "2006-09-13T18:57:55Z")

</div>

I tried to get a random child’s title from a page using this code, but  
it  
doesn’t work because it gives the title of the current page and not of  
the  
child. Is there a way to get a random page?

\<r:random\>  
\<r:children:each\>  
\<r:option\>\<r:title /\>\</r:option\>  
\</r:children:each\>  
\</r:random\>

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 13, 2006, 6:59pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/2 "2006-09-13T18:59:55Z")

</div>

Does this work?  
\<r:children:each limit=“1”\>  
\<r:random\>  
\<r:option\>\<r:title /\>\</r:option\>  
\</r:random\>  
\</r:children:each\>

That would be grabbing one random title from children pages, correct?  
I haven’t tried it myself, so I might be wrong.

Erik M.

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 13, 2006, 11:38pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/3 "2006-09-13T23:38:35Z")

</div>

The code you wrote should grab the title of the first child (in a list  
made  
by all the children ordered in ascending order by published\_at).

2006/9/13, Erik M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 13, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/4 "2006-09-13T23:44:36Z")

</div>

Ah. So something like this? If such a thing would work.

\<r:random limit=“1”\>  
\<r:children:each\>  
\<r:option\>\<r:title /\>\</r:option\>  
\</r:children:each\>  
\</r:random\>

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 13, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/5 "2006-09-13T23:50:36Z")

</div>

Unfortunately that is the same code I posted 🙂 (except for the limit  
attribute of random, that doesn’t exist).

2006/9/13, Erik M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 14, 2006, 6:15pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/6 "2006-09-14T18:15:17Z")

</div>

Yeah, I wrote that in a rush before leaving work and realized it a few  
minutes later. Sorry!

Erik

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 14, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/7 "2006-09-14T18:16:55Z")

</div>

I’ve been following this thread, but haven’t replied yet. The problem  
may  
be a tag scoping issue, and maybe a custom tag is in need here.

tag “children:random” do |tag|  
children = tag.locals.children  
if children.size \> 0  
index = rand(children.size)  
tag.locals.page = children[index]  
tag.expand  
end  
end

Then in your page, write

\<r:children:random /\>\<r:title /\>\</r:children:random\>

or just  
\<r:children:random:title /\>

This will only select one child page, mind you, and would need to be  
modified to select more than one.

Cheers,

Sean C.  
[seancribbs.com](http://seancribbs.com)

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 14, 2006, 10:33pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/8 "2006-09-14T22:33:43Z")

</div>

On 14/09/06, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Then in your page, write
> 
> \<r:children:random /\>\<r:title /\>\</r:children:random\>
> 
> or just  
> \<r:children:random:title /\>
> 
> This will only select one child page, mind you, and would need to be  
> modified to select more than one.

I’ve added this to the wiki at  
[http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags](http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags)

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 15, 2006, 12:50am UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/9 "2006-09-15T00:50:45Z")

</div>

On 14/09/06, Sean C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> FYI Peek at the weblog… [http://radiantcms.org/blog/](http://radiantcms.org/blog/)

That is awesome!

Thank you!

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![Giovanni\_I](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/4491bb/32.png) [@Giovanni\_I](https://www.ruby-forum.com/u/Giovanni_I)\
**Post date:** [September 14, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/r-random-and-page-children/68577/10 "2006-09-14T22:36:08Z")

</div>

FYI Peek at the weblog… [http://radiantcms.org/blog/](http://radiantcms.org/blog/)

Sean C.  
[seancribbs.com](http://seancribbs.com)
