# Menu and Page Menus

**URL:** <https://www.ruby-forum.com/t/menu-and-page-menus/69141>\
**Category:** Radiant CMS\
**Created:** [September 18, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141 "2006-09-18T23:19:11Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 18, 2006, 11:19pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/1 "2006-09-18T23:19:11Z")

</div>

Hello. My name is Ian G… I am new to Radiant and new to ruby.

I am trying to figure out how I can get the menu for pages I create to  
be autogenerated for me, like the breadcrumbs. I was thinking it would  
involve the radius tags and maybe the creation of a behavior so that  
if I create a page part say: sidebar then I could include a tag say  
\<r:menu /\> then I could make it so that it automatically creates a  
menu based on the pages from the root all the way down to the current  
page.

Anyway, let me know if anyone thinks this is worthwhile and how I  
might go about implementing it.

–  
Ian G.  
Ian G. Online  
[http://www.iangordon.us](http://www.iangordon.us)

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 19, 2006, 12:44am UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/2 "2006-09-19T00:44:51Z")

</div>

Hi Ian,

There is a recent thread dealing with this issue, you can check it out  
on the ruby-forum archive here [http://www.ruby-forum.com/topic/81640](http://www.ruby-forum.com/topic/81640)

No one has posted a complete solution yet, but there are a few ideas  
floating around in there and definately some interest in getting  
something made.

Kev

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 19, 2006, 12:49am UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/3 "2006-09-19T00:49:19Z")

</div>

On 18/09/06, Ian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Anyway, let me know if anyone thinks this is worthwhile and how I  
> might go about implementing it.

I’d be very interested to see what you come up with for this 🙂

Rather than a behaviour, perhaps a ‘Global Tag’ is more like what you  
want to make:

[http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags](http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags)

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 21, 2006, 12:47pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/4 "2006-09-21T12:47:32Z")

</div>

Dave C. wrote:

> I’d be very interested to see what you come up with for this 🙂
> 
> Rather than a behaviour, perhaps a ‘Global Tag’ is more like what you  
> want to make:
> 
> [http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags](http://dev.radiantcms.org/radiant/wiki/HowToDefineGlobalTags)
> 
> –  
> Regards,  
> Dave

I am too Dave, I am too…

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 21, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/5 "2006-09-21T14:41:44Z")

</div>

You may be able to modify the “sitemap” snippet that’s on the wiki to do  
just that, perhaps with only one global tag creation. I’ve been mulling  
around how to do this. I’ll get back to you. (I sense a new “how to”  
article.)

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

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 21, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/6 "2006-09-21T14:53:45Z")

</div>

Ok, I’ve got it. Let’s take the “sitemap” snippet from the wiki and  
modify  
it.

The part we’ll put in our page will look like this:

The sitemapper snippet, slightly modified:

\<r:if\_ancestor\_or\_self\>  
\<r:children:each by=“title” order=“asc”\>  
\<r:unless\_content part=“no-map”\>

-   
\<r:link /\>  

  
\<r:snippet name=“sitemapper” /\>  

  
  
\</r:unless\_content\>  
\</r:children:each\>  
\</r:if\_ancestor\_or\_self\>

Then our new global tag definition (I’m using the PageContext syntax):

define\_tag ‘if\_ancestor\_or\_self’ do |tag|  
page = tag.globals.page  
ancestors = tag.globals.page.ancestors  
context\_page = tag.locals.page  
tag.expand if context\_page == page or  
ancestors.include?(context\_page)  
end

This should, starting from the root page, create a contextual menu  
including  
the siblings of all of the parent pages and the children of the current  
page.

Let me know how that works out!

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

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 21, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/7 "2006-09-21T19:48:10Z")

</div>

Hi Guys,

Unfortunately there appears to be some core behaviour stopping Seans  
code above from working - see trac ticket  
[http://dev.radiantcms.org/radiant/ticket/450](http://dev.radiantcms.org/radiant/ticket/450)

I’ve put my own stopgap solution together, utilizing the logic that Sean  
posted:

* * *

class Behavior::Base  
class TagError \< StandardError; end

define\_tags do

```
tag 'menu' do |tag|
  url = tag.attr['url']
  if found = Page.find_by_url(tag.attr['url'])
    menu_root = found
  else
    menu_root = Page.find_by_url("/")
  end

  root_page = tag.locals.page
  ancestors = root_page.ancestors

  result = "<ul>\n"
  menu_root.children.each do |child|
    context_page = child
    result << recurse_menu(root_page, context_page, ancestors)
  end
  result << "</ul>\n"
end

```

end

## def recurse\_menu(root\_page, context\_page, ancestors) result = “
- \<a  
href="#{context\_page.url}"\>#{context\_page.title}\n”  
if context\_page.has\_children? && (root\_page == context\_page ||  
ancestors.include?(context\_page))  
result \<\< “
\n”  
context\_page.children.each do |child|  
result \<\< recurse\_menu(root\_page, child, ancestors)  
end  
result \<\< “
\n”  
end  
result \<\< “
\n”  
end  
end

If anyone thinks it would be worthwhile, I can see what I can do about  
packaging this up into a plugin.

Kevin A.

> **[Digital Marketing Agency Glasgow | Digital Blueprint | Scotland](https://www.digitalblueprint.co.uk/)**
>
> Digital Blueprint is a full-service creative design and digital marketing agency that helps visionary companies realise their strengths and elevate their brands, crafting authentic experiences that engage and inspire audiences at every touch point.

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 21, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/8 "2006-09-21T18:46:40Z")

</div>

Because of a limitation in the way that snippets are rendered, the code  
I  
posted earlier will not limit which children of the root of your sitemap  
are  
rendered. Essentially, everytime you get to the \<r:if\_ancestor\_or\_self\>  
tag, tag.globals.page will equal tag.locals.page. I intend to leave a  
more  
detailed ticket on the Trac.

Cheers,

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

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 21, 2006, 8:04pm UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/9 "2006-09-21T20:04:55Z")

</div>

On 21/09/06, Kevin A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If anyone thinks it would be worthwhile, I can see what I can do about  
> packaging this up into a plugin.

Yes please! 🙂

–  
Regards,  
Dave

---

<div class="post-metadata">

**Author:** ![dzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a9adbd/32.png) [@dzo](https://www.ruby-forum.com/u/dzo)\
**Post date:** [September 22, 2006, 10:50am UTC](https://www.ruby-forum.com/t/menu-and-page-menus/69141/10 "2006-09-22T10:50:55Z")

</div>

Kevin A. wrote:

> ---- truncated ----
> 
> If anyone thinks it would be worthwhile, I can see what I can do about  
> packaging this up into a plugin.

You don’t even have to ask man, just do it and if people like it  
awesome, if they don’t hey, at least you contributed.
