Skip to content

Instantly share code, notes, and snippets.

@riyad
Created February 28, 2012 17:38
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save riyad/1933884 to your computer and use it in GitHub Desktop.
Save riyad/1933884 to your computer and use it in GitHub Desktop.
How to make breadcrumbs_on_rails render a Bootstrap compatible breadcrumb navigation
# The BootstrapBreadcrumbsBuilder is a Bootstrap compatible breadcrumb builder.
# It provides basic functionalities to render a breadcrumb navigation according to Bootstrap's conventions.
#
# BootstrapBreadcrumbsBuilder accepts a limited set of options:
# * separator: what should be displayed as a separator between elements
#
# You can use it with the :builder option on render_breadcrumbs:
# <%= render_breadcrumbs :builder => ::BootstrapBreadcrumbsBuilder, :separator => "&raquo;" %>
#
# Note: You may need to adjust the autoload_paths in your config/application.rb file for rails to load this class:
# config.autoload_paths += Dir["#{config.root}/lib/"]
#
class BootstrapBreadcrumbsBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder
def render
@context.content_tag(:ul, class: 'breadcrumb') do
@elements.collect do |element|
render_element(element)
end.join.html_safe
end
end
def render_element(element)
current = @context.current_page?(compute_path(element))
@context.content_tag(:li, :class => ('active' if current)) do
link_or_text = @context.link_to_unless_current(compute_name(element), compute_path(element), element.options)
divider = @context.content_tag(:span, (@options[:separator] || '/').html_safe, :class => 'divider') unless current
link_or_text + (divider || '')
end
end
end
@SaladFork
Copy link

Updated for Bootstrap 4 (alpha6): https://gist.github.com/SaladFork/270a4cb3ac20be9715b7117551c31ec7

Of note:

  • No longer has :separator option (Bootstrap 4 accomplishes this with CSS)
  • Now has :container_tag option in addition to :tag option (default to :ol and :li, respectively)
  • Now has :show_empty option which defaults to false to prevent rendering when there are no crumbs (h/t @mrfoto's fork)

@sadjow
Copy link

sadjow commented Mar 12, 2022

Bootstrap 5

# The BootstrapBreadcrumbsBuilder is a Bootstrap compatible breadcrumb builder.
# It provides basic functionalities to render a breadcrumb navigation according to Bootstrap's conventions.
#
# You can use it with the :builder option on render_breadcrumbs:
#     <%= render_breadcrumbs :builder => ::BootstrapBreadcrumbsBuilder %>
#
# Note: you can put this file into app/utils/bootstrap_breadcrumbs_builder.rb and it will autoload.
#
class BootstrapBreadcrumbsBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder
  def render
    @context.content_tag(:nav, 'aria-label' => "breadcrumb") do
      @context.content_tag(:ol, class: 'breadcrumb') do
        @elements.collect do |element|
          render_element(element)
        end.join.html_safe
      end
    end
  end

  def render_element(element)
    current = @context.current_page?(compute_path(element))

    classes = ['breadcrumb-item']
    classes.push('active') if current

    @context.content_tag(:li, :class => classes.join(" ") ) do
      link_or_text = @context.link_to_unless_current(compute_name(element), compute_path(element), element.options)
      link_or_text
    end
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment