How to get current_page.parent.path from traversal (breadcrumbs)

I’m trying to create a breadcrumb that will link to the parent index page. After searching, it seems that

(edit: 4 spaces so this would show up and have code styling. need text here or edit will not save…)

<%= current_page.parent.path %>

may get me what I want from the traversal extension. I would like this to return the parent link and parent title if possible.

It seems like the traversal extension may depend on sitemap, so I tried putting this in my config.rb

activate :sitemap
activate :traversal

but that didn’t work.

I have a partial example of what I want by placing the following code in my layout.erb file

<ul class="breadcrumb">
<li><%= link_to 'Home', '/' %></li><span> ></span>
<li><%= link_to 'Back', 'index.html' %></li>
</ul>

But this doesn’t help with the parent pages title.

Unfortunately I’m new to middleman and ruby and cant figure how to implement this for my site.

My setup

  • Windows 8 x64
  • Ruby 1.9.3 latest
  • Middleman 3.2.2
  • Basic middleman implementation with a custom template defined by a source folder and config.rb file

Try <%= current_page.parent.data.title %>

1 Like

Thank you for that. It now works! Here is how I modified my example case from above:

<ul class="breadcrumb">
<li><%= link_to 'Home', '/' %></li><span> ></span>
<% if current_page.parent %>
<li><%= link_to current_page.parent.data.title, current_page.parent %></li>
<% end %>
</ul>

You need the if condition if the code will be passed into the index file template. It will cause an error building the index file if you don’t include it.

1 Like

I must modify my advice.

Instead of accessing the title through data you are better of using metadata, like this

current_page.parent.metadata[:page]['title']

Clumsier syntax to get an identical result, but it will give you more flexibility. data.title is frozen and unmodifiable, while metadata[:page]['title'] can be modified by a script, should you ever need.

Got it. Thanks again.

I have been trying to adapt this code to work with Bootstrap’s included styling on a larger site.

I was wondering if you came across an issue with “index.html” pages within a folder where it will duplicate the Home link leading to a breadcrumb that looks like:

Home / Home / Page Name

Has anyone else come across this?

I have tried the middleman-breadcrumbs extension but that cannot be easily styled.

I came across a different but similar issue, where looping the parents does not finishes at root /index.html but earlier. This happens in my own code and in the https://github.com/marnen/middleman-breadcrumbs extension too. The problem shows up when there is the following structure:

  • index.html
  • shop/some_category.html
  • shop/some_category/some_subcategory.html
  • shop/some_category/some_subcategory/product1.html

Pay attention: there is no /shop.html page, which apparently breaks the link in the parents-chain. Interestingly, the developer-server sitemap link shows the correct file-structure.

You can extract the main code from this extension, make it just a simple helper and modify the code to suit your needs. The code is an amazing 3-liner that’ve put my lame code to shame.

  def breadcrumbs(page)
    hierarchy = [page]
    hierarchy.unshift hierarchy.first.parent while hierarchy.first.parent
    hierarchy.collect {|page| link_to page.data.title, "/#{page.path}" }.join(h ' > ')
  end

I’ve updated middleman-breadcrumbs. As of v0.3.0, you can wrap each element in a tag and use custom separators. See https://github.com/marnen/middleman-breadcrumbs/issues/2.