SiteMap

Description

SiteMap provides a way to model out your site’s views in a hierarchal fashion. You configure your views and then it builds a tree structure from it that is easy to lookup specific nodes and also traverse their relationships, such as parents or children.

Installation

gem install site_map

Usage

Basic Definition

SiteMap provides a simple syntax for defining a site’s views. Add the following to config/site_map.rb (site_map will load this when required) or an initializer for your app:

require 'site_map'
SiteMap.define do |map|
  map.view :home
  map.view :about
  map.view :contact
end

This defines three views home, about and contact. The view method is the most basic way to configure your site map and only requires a unique index as it’s first parameter. It simply creates within the site map a view node with the index provided (in this case home, about or contact).

Setting Attributes

Every site map view node also has a label, url and visible attribute. The label is the name of the page the node represents and the url is how you navigate to that page. The visible attribute specifies whether this node should be shown. Using these options is simple:

require 'site_map'
SiteMap.define do |map|
  map.view(:home, {
    :label => "Home :app_name",
    :url => "'/home'"
  })
  map.view(:about, {
    :url => "about_path"
  })
  map.view(:contact, {
    :visible => "can_contact?"
  })
end

So same basic configuration, but I’ve added custom options for the previously mentioned attributes. Firstly, url and visible attributes are intended to be eval’d within the view space (scope) of an app. This allows you to specify very custom ways to determine a path or check if a node should be shown. Label’s are not intended to be eval’d, but are dynamic in a different way, by subbing out symbol names with an options hash, again in the view space (scope). This is explained more and used within site map’s view helpers.

SiteMap also has other ways to define your site map. Check out the wiki for more help: wiki.github.com/jcredding/site_map/

Copyright © 2010 John Collin Redding See the attached MIT License.