Skip to content

Instantly share code, notes, and snippets.

@lappi-lynx
Last active August 29, 2015 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lappi-lynx/8934476 to your computer and use it in GitHub Desktop.
Save lappi-lynx/8934476 to your computer and use it in GitHub Desktop.
bootstrap builder for breadcrumbs_on_rails gem
# bootstrap builder for breadcrumbs_on_rails gem
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)
active = element.path.nil? || @context.current_page?(compute_path(element))
# Bootstrap use '/' divider by default but you can customize it:
# divider = @context.content_tag(:span, '/'.html_safe, class: 'divider') unless active
@context.content_tag(:li, :class => ('active' if active)) do
content = if element.path.nil?
compute_name(element)
else
@context.link_to_unless_current(compute_name(element), compute_path(element), element.options)
end
# content + (divider || '')
content
end
end
end
# Usage:
# = render_breadcrumbs(builder: BootstrapBreadcrumbsBuilder)
@krsyoung
Copy link

Thanks @extazystas, this was very helpful! One tweak that I found useful was to add a check that there are indeed elements to render (this avoids having an empty looking <ul>)

 def render
    return "" if @elements.count == 0
    @context.content_tag(:ul, class: 'breadcrumb') do 
        @elements.collect do |element|
            render_element(element)
        end.join.html_safe
    end
end

Putting this code in an initializer works like a charm.

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