Skip to content

Twitter Bootstrap integration

mjtko edited this page Mar 12, 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 (from @hmasing)

The second approach is to let jQuery do the work:

/* ==========================================================
 * 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');
$('#navigation_tabs ul li.dropdown').children('a').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');

jQuery + CoffeeScript approach (from @weblee)

If you would like jQuery to do the navigation menu you can do. (im using coffeescript)

$('#navigation ul li ul').addClass('dropdown-menu')
$('#navigation ul li ul').parent().addClass('dropdown')
$('#navigation ul li.dropdown').children('a').addClass('dropdown-toggle')
$('#navigation ul li.dropdown').children('a').html (index, text) =>
  return text + '<b class="caret"></b>' 
$('#navigation ul li ul').prev().attr('data-toggle','dropdown')
$('#navigation ul li ul').children('li').removeClass('active')

Topbar navigation

See this gist of a custom renderer.

Clone this wiki locally