# Radiant site map

**URL:** <https://www.ruby-forum.com/t/radiant-site-map/63984>\
**Category:** Radiant CMS\
**Created:** [June 20, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984 "2006-06-20T23:23:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 20, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/1 "2006-06-20T23:23:25Z")

</div>

Does anyone know of a way to generate a complete site map for a  
Radiant powered site (all children, sub-children, etc.)? The  
acts\_as\_tree that controls the site heirarchy only seems to be able to  
query the immediate children of a node, and I can’t seem to work out a  
recursive function that would do the job … any help would be  
appreciated!

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 22, 2006, 5:47am UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/2 "2006-06-22T05:47:40Z")

</div>

Nathan W. wrote:

> Does anyone know of a way to generate a complete site map for a  
> Radiant powered site (all children, sub-children, etc.)? The  
> acts\_as\_tree that controls the site heirarchy only seems to be able to  
> query the immediate children of a node, and I can’t seem to work out a  
> recursive function that would do the job … any help would be  
> appreciated!

You could create a tag or set of tags on a behavior to do this for you.  
What would you like to do? Create a nested unordered list?

Try this:

class SiteMapBehavior \< Behavior::Base

```
 register "Site Map"

 define_tags do
   tag "tree" do |tag|
     page = tag.locals.page
     tree_for(page)
   end
 end

 private

   def tree_for(page)
     list = ""
     page.children.each do |child|
       list << "<li><a href="#{ child.url }">#{ child.title }</a>#{
         tree_for(child) }</li>"
     end
     "<ul>#{ list }</li>" unless list.empty?
   end

```

end

And then on the page part:

\<r:find url=“/”\>\<r:tree /\>\</r:find\>

The code above is completely untested, but should get you going in the  
general direction.

Hope that helps.

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

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 22, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/3 "2006-06-22T16:22:29Z")

</div>

John W. Long wrote:

> The code above is completely untested, but should get you going in the  
> general direction.

There were a few typos:

class SiteMapBehavior \< Behavior::Base

```
 register "Site Map"

 define_tags do
   tag "tree" do |tag|
     page = tag.locals.page
     tree_for(page)
   end
 end

 private

   def tree_for(page)
     list = ""
     page.children.each do |child|
       list << %{<li><a href="#{ child.url }">#{ child.title
         }</a>#{tree_for(child) }</li>} unless child.virtual?
     end
     "<ul>#{ list }</ul>" unless list.empty?
   end

```

end

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

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 22, 2006, 5:05pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/4 "2006-06-22T17:05:51Z")

</div>

Thank you John. These behaviour “pearls” from you are very useful for  
a modest user like me … 🙂

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 22, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/5 "2006-06-22T17:45:13Z")

</div>

A couple of things … first, you are my _hero_ … it worked  
perfectly! This was the last bit keeping me from finishing a site.  
Thank you (ad nauseam). Second, it’s at times like these that I wish I  
understood better how things work inside of radiant.

If you have time, could you explain briefly what is “tag.locals” is?  
For instance, when I call

raise tag.inspect

I get a dump of all parts of all pages on the site. Calling

raise tag.locals.inspect

seems to call a sub-set of the same information, but with no  
distinguishing rhyme or reason (on my site, using the above raise  
dumps the page for the site map, the home page, and the news and  
events page, but no other pages).

Also, the “secret sauce” behind the sitemap behavior seems to be in  
the “child.virtual?” declaration. Can you explain what the “virtual”  
method is?

Thanks, and thanks again for the sitemap behaviour!

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 22, 2006, 6:11pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/6 "2006-06-22T18:11:14Z")

</div>

To add the home page to the site map, I simply replaced the line

tree\_for(page)

with the line

%{

- #{page.title}#{tree\_for(page)}

}

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 22, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/7 "2006-06-22T18:04:44Z")

</div>

A word of note: the site map behavior doesn’t create a link for the  
home page …

---

<div class="post-metadata">

**Author:** ![laserwhit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/c6cbf5/32.png) [@laserwhit](https://www.ruby-forum.com/u/laserwhit)\
**Post date:** [June 22, 2006, 9:30pm UTC](https://www.ruby-forum.com/t/radiant-site-map/63984/8 "2006-06-22T21:30:43Z")

</div>

Nathan W. wrote:

> A couple of things … first, you are my _hero_ … it worked  
> perfectly! This was the last bit keeping me from finishing a site.  
> Thank you (ad nauseam).

Awesome.

> Second, it’s at times like these that I wish I  
> understood better how things work inside of radiant.
> 
> If you have time, could you explain briefly what is “tag.locals” is?

Tag#locals is sort of like scope for tags. Think of it as where you  
define local variables for a tag. There is also the corresponding  
Tag#globals which is for defining and using variables that you want  
accessible to all tags.

See the Radius Quickstart guide for more info:

[http://radius.rubyforge.org/files/QUICKSTART.html](http://radius.rubyforge.org/files/QUICKSTART.html)

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