# Radiant Free-for-all: Navigation Menus

**URL:** <https://www.ruby-forum.com/t/radiant-free-for-all-navigation-menus/107469>\
**Category:** Radiant CMS\
**Created:** [June 22, 2007, 1:17am UTC](https://www.ruby-forum.com/t/radiant-free-for-all-navigation-menus/107469 "2007-06-22T01:17:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [June 22, 2007, 1:17am UTC](https://www.ruby-forum.com/t/radiant-free-for-all-navigation-menus/107469/1 "2007-06-22T01:17:04Z")

</div>

In the spirit of The Rails Way Free-for-all (http://  
[www.therailsway.com/2007/6/4/free-for-all-tab-helper](http://www.therailsway.com/2007/6/4/free-for-all-tab-helper)), I thought it  
would be fun to see how we all handle making dynamic navigation for  
Radiant sites.

PROBLEM: I have a hierarchical site, where each page has several  
children, and some children also have children. I want a standard  
left-hand navigation menu. It should be automatically generated. The  
navigation menu should show the children for only the current page,  
and it should expand to show the parents and ancestors of the current  
page.

Example:

- Home
  - About

  - Articles
    - Article 1 – you are here

    - Article 2

  - Widgets
  - Hoozits
  - Whatzits

(- expanded pages)  
(+ unexpanded pages)

If you’ve done this in Radiant, please post your solution. See mine  
in action at [http://radiant.artofmission.com/](http://radiant.artofmission.com/) Username: demo,  
password: radiant. Also see [http://biola.artofmission.com](http://biola.artofmission.com) (under  
construction)

Pro:

- Easy & foolproof - content editors don’t even have to think about  
navigation - it just happens for them.
- Automatic - new pages show up in nav automatically.

Con:

- Slow as molasses. On [http://biola.artofmission.com](http://biola.artofmission.com), regenerating a  
page can take as long as 2 seconds.

Here’s my code:

== Snippets ==

=== Nav ===

 \<li class="current"\>Home 
 
 

=== sub-nav ===  
\<r:unless\_content part=“no-map”\>  
\<li\<r:if\_self\> class=“current”\</r:if\_self\>\>\<r:breadcrumb /\>  
\<r:if\_children\>  
\<r:if\_ancestor\_or\_self\>

  
\<r:children:each\>  
\<r:snippet name=“sub-nav” /\>  
\</r:children:each\>  

  
\</r:if\_ancestor\_or\_self\>  
\</r:if\_children\>

== Tags ==  
[http://svn.artofmission.com/svn/plugins/radiant/plugins/navigation\_tags](http://svn.artofmission.com/svn/plugins/radiant/plugins/navigation_tags)

require ‘standard\_tags’ # make sure we have the previous definition  
first

module StandardTags

include Radiant::Taggable

# Inspired by this thread:

# [http://www.mail-archive.com/removed\_email\_address@domain.invalid/](http://www.mail-archive.com/removed_email_address@domain.invalid/)

msg03234.html  
desc %{  
Renders the contained element if the current item is an ancestor  
of the current page or if it is the page itself.  
}  
tag “if\_ancestor\_or\_self” do |tag|  
tag.expand if tag.globals.actual\_page.url.starts\_with?  
(tag.locals.page.url)  
end

desc %{  
Renders the contained element if the current item is also the  
current page.  
}  
tag “if\_self” do |tag|  
tag.expand if tag.locals.page == tag.globals.actual\_page  
end

desc %{  
Renders the contained elements only if the current contextual  
page has children.

```
 *Usage:*
 <pre><code><r:if_children>...</r:if_children></code></pre>

```

}  
tag “if\_children” do |tag|  
children = tag.locals.page.children  
tag.expand if children.size \> 0  
end

end

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [June 22, 2007, 10:42am UTC](https://www.ruby-forum.com/t/radiant-free-for-all-navigation-menus/107469/2 "2007-06-22T10:42:50Z")

</div>

> - Slow as molasses. On [http://biola.artofmission.com](http://biola.artofmission.com), regenerating a  
> page can take as long as 2 seconds.

```
 Are you running radiant/rails through CGI?

/AITOR

```

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [June 22, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/radiant-free-for-all-navigation-menus/107469/3 "2007-06-22T14:13:21Z")

</div>

> ```
> Are you running radiant/rails through CGI?
> 
> ```

No, it’s running on Mongrel.

I’ve been benchmarking the script - the slowness is coming from  
recursively calling \<r:snippet name=“sub-nav” /\>. Each time the sub-  
nav snippet is called takes about 0.05 - 0.1 seconds. For a large  
site, that adds up to a very slow load time. I’m not sure yet if it’s  
rendering the snippet that takes so long, or if it’s my tag that’s  
inefficient.

On Jun 22, 2007, at 3:41 AM, Aitor Garay-Romero wrote:

> > In the spirit of The Rails Way Free-for-all (http://
> > 
> > navigation - it just happens for them.  
> > === Nav ===
> > 
> > desc %{  
> > current page.
> > 
> > ```
> > <r:if_children>…</r:if_children>
> > ```
> > 
> > * * *

* * *

Ryan H.  
Art of Mission, Inc.  
3720 Gattis School Rd #800 PMB 245  
Round Rock, TX 78664

800-722-1492 (phone)

[www.artofmission.com](http://www.artofmission.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [June 22, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/radiant-free-for-all-navigation-menus/107469/4 "2007-06-22T15:30:36Z")

</div>

One thing John and I discussed last night in a “duh!” moment was the  
fact that there is not an index on the ‘parent\_id’ column of the pages  
table. Adding that might give a minor performance boost.

Sean

---

<div class="post-metadata">

**Author:** ![rpheath](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/73ab20/32.png) [@rpheath](https://www.ruby-forum.com/u/rpheath)\
**Post date:** [June 22, 2007, 8:59pm UTC](https://www.ruby-forum.com/t/radiant-free-for-all-navigation-menus/107469/5 "2007-06-22T20:59:23Z")

</div>

Wow, this is a good idea. I will try adding an index and see if it  
helps.

On Jun 22, 2007, at 8:29 AM, Sean C. wrote:

> > site, that adds up to a very slow load time. I’m not sure yet if it’s

* * *

Ryan H.  
Art of Mission, Inc.  
3720 Gattis School Rd #800 PMB 245  
Round Rock, TX 78664

800-722-1492 (phone)

[www.artofmission.com](http://www.artofmission.com)  
removed\_email\_address@domain.invalid
