Skip to content

Instantly share code, notes, and snippets.

@choan
Created January 22, 2011 01:31
Show Gist options
  • Save choan/790753 to your computer and use it in GitHub Desktop.
Save choan/790753 to your computer and use it in GitHub Desktop.
Recursive layout for generating nanoc menus
<% raise ArgumentError, "The @collection has no items to build the ToC." if @collection.empty? %>
<% @depth ||= -1 %>
<% @depth -= 1 if @depth > 0 %>
<% @reject ||= lambda { |x| false } %>
<% @sort_by ||= lambda { |x| x[:weight] || 0 } %>
<% @label ||= lambda { |x| x[:title] } %>
<% collection = @collection.reject(&@reject) %>
<% unless collection.empty? %>
<ul>
<% collection.sort_by(&@sort_by).each do |child| %>
<li<%= ' class="active"' if @item == child %>>
<% unless @item == child %>
<a href="<%= child.path %>"><%= @label.call(child) %></a>
<% else %>
<strong><%= child[:title] %></strong>
<% end %>
<% children = child.children.reject(&@reject) %>
<%= render(layout.identifier, :collection => children, :depth => @depth, :reject => @reject, :sort_by => @sort_by, :label => @label) if @depth != 0 && !children.empty? %>
</li>
<% end %>
</ul>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment