Skip to content

Instantly share code, notes, and snippets.

@jalberto
Created April 16, 2012 22:33
Show Gist options
  • Save jalberto/2402087 to your computer and use it in GitHub Desktop.
Save jalberto/2402087 to your computer and use it in GitHub Desktop.
simple-navigation render for twiter bootstrap nav-list
class ListBootstrap < SimpleNavigation::Renderer::Base
def render(item_container)
list = item_container.items.inject([]) do |list, item|
if item.html_options[:opts]
if item.html_options[:opts][:nav_header]
list << li_header(item)
elsif item.html_options[:opts][:icon]
list << li_icon(item)
end
else
list << li_tag(item)
end
end
list.join.html_safe
end
private
def li_tag(item)
classes = item.selected? ? 'active' : nil
content_tag(:li, link_to(item.name, item.url, {:method => item.method}.merge(item.html_options.except(:class,:id,:opts))), { class: classes })
end
def li_header(item)
content_tag(:li, item.name, { class: 'nav-header' }.merge(item.html_options.except(:class,:id,:opts)))
end
def li_icon(item)
classes = item.selected? ? 'active' : nil
content_tag(:li, content_tag(:a, content_tag(:i, nil, :class => item.html_options[:opts][:icon]) + item.name, {:href => item.url, :method => item.method}.merge(item.html_options.except(:class,:id,:opts))), { class: classes })
end
end
navigation.items do |primary|
primary.item :header_admin, 'Admin', '#', :opts => {:nav_header => true}
primary.item :my_items, 'My Items', items_path, :opts => {:icon => 'icon-th'}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment