# Google sitemap.xml support + ActionView helpers in behaviors

**URL:** <https://www.ruby-forum.com/t/google-sitemap-xml-support-actionview-helpers-in-behaviors/97879>\
**Category:** Radiant CMS\
**Created:** [July 11, 2006, 12:35am UTC](https://www.ruby-forum.com/t/google-sitemap-xml-support-actionview-helpers-in-behaviors/97879 "2006-07-11T00:35:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [July 11, 2006, 12:35am UTC](https://www.ruby-forum.com/t/google-sitemap-xml-support-actionview-helpers-in-behaviors/97879/1 "2006-07-11T00:35:38Z")

</div>

Hi folks,

I added a tiny extra chunk of code in my SiteController to generate a  
sitemap.xml file (  
[http://www.google.com/support/webmasters/bin/topic.py?topic=8467](http://www.google.com/support/webmasters/bin/topic.py?topic=8467) )  
from the list of all published pages.

If there’s any wider interest for such a feature, I could package it  
as a Behavior.

Is there any way (or plan) to inject default ActionView helper method  
(such as link\_to, …) to Radiant behaviors? I doubt a simple mix-in  
would do the trick but I admit I haven’t looked at it yet. Wouldn’t  
that be terrific if behaviors had access to all Rails bells and  
whistles? Especially when some custom Radius tags generate more  
complex output…

Cheers,

## Xavier

[http://defrang.com](http://defrang.com)

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [July 11, 2006, 1:11am UTC](https://www.ruby-forum.com/t/google-sitemap-xml-support-actionview-helpers-in-behaviors/97879/2 "2006-07-11T01:11:02Z")

</div>

Xavier wrote:

> Hi folks,
> 
> I added a tiny extra chunk of code in my SiteController to generate a  
> sitemap.xml file (  
> [http://www.google.com/support/webmasters/bin/topic.py?topic=8467](http://www.google.com/support/webmasters/bin/topic.py?topic=8467) )  
> from the list of all published pages.
> 
> If there’s any wider interest for such a feature, I could package it  
> as a Behavior.

Definitely. I just did something similar, already set up as a behavior,  
but it’s hardcoded to my site at the moment, while I tried to figure out  
how to get at the full URL…

–

class SiteMapBehavior \< Behavior::Base

```
 register "Sitemap"

 description %{
   Creates a Google sitemap.
 }

 define_tags do
   tag "sitemap" do |tag|
     root = Page.find_by_parent_id(nil)
     sitemap_header
     sitemap_for(root)
     sitemap_footer
   end
 end

```

private

```
 def sitemap_for(page)
   sitemap_url(page.url, page.published_at)

   page.children.each do |child|
     if child.published? and not child.virtual?
       sitemap_for(child)
     end
   end
 end

 def sitemap_header
   @sitemap = '<?xml version="1.0" encoding="UTF-8"?>' + "\n" +
     '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">' +

```

“\n”

```
   @sitemap << DummyController.new(self).url_for("/testing")
 end

 def sitemap_footer
       @sitemap << "</urlset>"
 end

 def sitemap_url(url, published_at)
   @sitemap << "<url>\n"

   full_url = "http://www.wellesleycarriagehouse.com#{url}"
   @sitemap << " <loc>#{full_url}</loc>\n"

   if published_at
     pub_date = published_at.strftime("%Y-%m-%d")
     @sitemap << " <lastmod>#{pub_date}</lastmod>\n"
   end

   @sitemap << "</url>\n"
 end

```

end

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [July 11, 2006, 9:56am UTC](https://www.ruby-forum.com/t/google-sitemap-xml-support-actionview-helpers-in-behaviors/97879/3 "2006-07-11T09:56:38Z")

</div>

> Definitely. I just did something similar, already set up as a behavior,  
> but it’s hardcoded to my site at the moment, while I tried to figure out  
> how to get at the full URL…

That’s cool.

ActionView helpers provides all the tool you need to build your URL in  
the cleanest fashion, that’s exactly why I asked about the  
availability of helpers to Behaviors in the same message. 🙂  
And that’s also why I did not implemented it as a Behavior in the first  
place…

Cheers,

Xavier

---

<div class="post-metadata">

**Author:** ![xavier](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/2bfe46/32.png) [@xavier](https://www.ruby-forum.com/u/xavier)\
**Post date:** [September 23, 2006, 10:25pm UTC](https://www.ruby-forum.com/t/google-sitemap-xml-support-actionview-helpers-in-behaviors/97879/4 "2006-09-23T22:25:52Z")

</div>

On 7/11/06, Jay L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Xavier wrote:
> 
> > If there’s any wider interest for such a feature, I could package it  
> > as a Behavior.

I was looking for adding support for google sitemap to a site and I  
found  
this thread. Once I’ll have implemented it, I’d like to add a recipe  
(How  
to) for a google sitemap behavior/code to the wiki. Is the code found in  
this thread OK? Did someone develop anything better or updated it  
recently?  
Thank you.
