Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
`breadcrumbs_on_rails` builder with Bootstrap 4 compatible output
# `Bootstrap4BreadcrumbsBuilder `is a Bootstrap 4 (alpha6) compatible breadcrumb
# builder. It is designed to work with the `breadcrumbs_on_rails` gem as a
# drop-in builder replacement.
#
# Bootstrap4BreadcrumbsBuilder accepts a limited set of options:
#
# | option | default | description |
# | ---------------- | ------- | ------------------------------------------ |
# | `:container_tag` | `:ol` | What tag to use for the list container |
# | `:tag` | `:li` | What HTML tag to use for breadcrumb items |
# | `:show_empty` | `false` | Whether to render container when no crumbs |
#
# You can use it by passing it to the `:builder` option on `render_breadcrumbs`:
#
# <%= render_breadcrumbs builder: ::Bootstrap4BreadcrumbsBuilder %>
#
# You will need to place this class in a location that is loaded by Rails. One
# suggested is `lib/bootstrap4_breadcrumbs_builder.rb`. You may need to adjust
# Rails' load path in `config/application.rb`:
#
# config.eager_load_paths << Rails.root.join('lib')
#
#
# See also:
# <https://v4-alpha.getbootstrap.com/components/breadcrumb/>
#
# Based on:
# - BreadcrumbsOnRails::Breadcrumbs::SimpleBuilder
# <https://github.com/weppos/breadcrumbs_on_rails/blob/v3.0.1/lib/breadcrumbs_on_rails/breadcrumbs.rb#L79>
# - BootstrapBreadcrumbsBuilder
# <https://gist.github.com/riyad/1933884>
#
class Bootstrap4BreadcrumbsBuilder < BreadcrumbsOnRails::Breadcrumbs::Builder
def render
return '' unless should_render?
container_tag = @options.fetch(:container_tag, :ol)
@context.content_tag container_tag, class: 'breadcrumb' do
@elements.collect do |element|
render_element(element)
end.join.html_safe
end
end
def render_element(element)
name = compute_name(element)
path = compute_path(element)
current = @context.current_page?(path)
item_tag = @options.fetch(:tag, :li)
@context.content_tag(item_tag, class: ['breadcrumb-item', ('active' if current)]) do
@context.link_to_unless_current(name, path, element.options)
end
end
private
def should_render?
@elements.any? || @options[:show_empty]
end
end
@iamraffe

This comment has been minimized.

Copy link

@iamraffe iamraffe commented Nov 20, 2017

Faaaaaantastic man!

@joemasilotti

This comment has been minimized.

Copy link

@joemasilotti joemasilotti commented Jan 22, 2018

Thanks for this! I made a small modification to include the recommended aria accessibility tags in my fork.

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