Skip to content

Twitter Bootstrap integration

tmaier edited this page Jan 2, 2012 · 6 revisions

See #72 for more discussion on this topic.

Breadcrumbs

Two approaches have been suggested.

Custom renderer

The first is to write a custom renderer for breadcrumbs (thanks @JeanMertz):

class BootstrapBreadcrumbs < SimpleNavigation::Renderer::Base

  def render(item_container)
      content_tag(:ul, li_tags(item_container).join(join_with), { id: item_container.dom_id, class: "#{item_container.dom_class} breadcrumb" })
  end

  protected

  def li_tags(item_container)
    item_container.items.inject([]) do |list, item|
      if item.selected?
        if include_sub_navigation?(item)
          list << content_tag(:li, link_to(item.name, item.url, {method: item.method}.merge(item.html_options.except(:class,:id)))) if item.selected?
          list.concat li_tags(item.sub_navigation)
        else
          list << content_tag(:li, item.name, { class: 'active' }) if item.selected?
        end
      end
      list
    end
  end

  def join_with
    @join_with ||= options[:join_with] || '<span class="divider">/</span>'.html_safe
  end
end

The jQuery approach

The second approach is to let jQuery do the work (thanks @hmasing):

/* ==========================================================
 * Attach bootstrap tab behavior to the tabbed navigation
 * ========================================================== */
$('#navigation_tabs ul').first().addClass('tabs');
$('#navigation_tabs ul li ul').addClass('dropdown-menu');
$('#navigation_tabs ul li ul').parent().addClass('dropdown');
$('li.dropdown a').first().addClass('dropdown-toggle');

/* Remove the active class from sublist items - the styles don't accomodate it */
$('ul.tabs ul.dropdown-menu li.active').removeClass('active');

/* Add the classes to trigger the bootstrap JS behavior */
$('#navigation_tabs ul li ul').parent().attr('data-dropdown','dropdown');
$('ul.tabs').attr('data-tabs','tabs');

Topbar navigation

See this gist of a custom renderer.

Clone this wiki locally