Navigation and Family Relationships
The default templates for Blueprint use the method render_in_hierarchy to generate a navigation tree containing all visible pages on your site in nested lists down as far as your hierarchy goes. The code looks like this:
<ul class="globalNav">
<% Page.protozoans.each do |page| %>
<%= render_page_in_hierarchy(page) { |p|
if (p == @page)
link_to_page(p.title, p, {}, {:class => "active"})
else
link_to_page(p.title, p)
end
} %>
<% end %>
</ul>
This is a bit of a blunt instrument though; most navigation systems require a bit more finesse. To create your site navigation, it will help to be aware of the Blueprint methods governing 'family relationships' (pro tip: you can view all the method definitions in app/models/page.rb).
The most important is Page.protozoans, which gives you an array of all the visible top-level pages in your site. A useful simple navigation pattern is:
<ul>
<% Page.protozoans.each do |page| %>
<%= content_tag(
"li",
link_to_page(page.title, page),
:class => page == @page ? "active" : nil
)
<% end %>
</ul>
It gives you list-items containing links to each of your top-level pages, with the current page (@page) given a CSS class of 'active' so that it might be highlighted.
Some other useful methods are:
children gives you all the children of a page. Usage example:
<% @page.children.each do |child| %>
<li><%= link_to_page(child.title, child) %></li>
<% end %>
(this displays list items with links to each child of the current page).
parent is the immediate parent of any page.
line gives you an array of all the pages in the same direct line as a specified page (page.line.size tells you how many pages there are, and page.line.first will give you the protozoan at the top of the line).
siblings will give you an array of all the pages on the same level with the same parent as the current page. In this example
- Top-level page
- Child page
- Another child page
- Yet another child page
- Another top-level page
all the 'child' pages are siblings of one another.
Not sure if this was already alluded to but....
One thing that has come up a few times with CSS people I have worked with is unique class names for nav items. Some sites we have done at portable have had images set for each nav items and CSS artists want to set this with CSS. My answer to this is to use the page slug as the css class. That was you can ensure that each class will be unique.
So something like the following could work...
This code will put the slug in the class as well as the active class.
Happy blueprinting