I\'m aware that there are single-level breadcrumbs in http://raphinou.github.com/jekyll-base/ but I\'m looking for some good ways to have breadcrumbs on a Jekyll sit
And I have improved JoostS's response to have the following capabilities:
If one exists, use the breadcrumb:
from the front matter of the final page
{% assign crumbs = page.url | remove:'/index.html' | split: '/' %}
<a href="/">Home</a>
{% for crumb in crumbs offset: 1 %}
{% if forloop.last %}
{% if page.breadcrumb != '' %}
▶ <a href="#">{{ page.breadcrumb }} </a>
{% else %}
▶ <a href="#">{{ crumb | replace:'-',' ' | remove:'.html' | capitalize }}</a>
{% endif %}
{% else %}
▶ <a href="{% assign crumb_limit = forloop.index | plus: 1 %}{% for crumb in crumbs limit: crumb_limit %}{{ crumb | append: '/' }}{% endfor %}">{{ crumb | replace:'-',' ' | remove:'.html' | capitalize }}</a>
{% endif %}
{% endfor %}
Here's my solution, which works with Jekyll 3.1.3 and GitHub Pages as of today. Like some solutions others have given, it simply inspects the page's URL and builds the breadcrumbs from it.
{% unless page.hide-breadcrumbs %}
<ul class="breadcrumb">
<li><a href="/">{{site.title}}</a> </li>
{% assign crumbs = page.url | split: '/' %}
{% for crumb in crumbs offset:1 %}
<li {% if forloop.last %}class="active"{% endif %}>
{% unless forloop.last %}
<a href="/{% for crumb in crumbs offset:1 limit:forloop.index %}{{crumb}}/{% endfor %}">
{{ crumb | capitalize }}
</a>
{% else %}
{{ crumb | capitalize }}
{% endunless %}
</li>
{% endfor %}
</ul>
{% endunless %}
I found an alternative technique that is not entirely automatic but that works on GitHub pages.
It consists of creating a data file with the list of possible paths.
For example, here is _data/breadcrumbs.csv
for my site:
url,title
/,Home
/api/,API
/api/jsonarray/,JsonArray
/api/jsonbuffer/,JsonBuffer
/api/jsonobject/,JsonObject
/api/jsonvariant/,JsonVariant
/doc/,Manual
/example/,Examples
/news/,News
/faq/,FAQ
Then, a simple loop creates the breadcrumb:
<ol class="breadcrumb">
{% for crumb in site.data.breadcrumbs %}
{% assign url_prefix = page.url | slice: 0, crumb.url.size %}
{% if (url_prefix == crumb.url) and (page.url != crumb.url) %}
<li class="breadcrumb-item">
<a href="{{ crumb.url | prepend: site.baseurl }}">
{{ crumb.title }}
</a>
</li>
{% endif %}
{% endfor %}
<li class="breadcrumb-item active">
<a href="{{ page.url | prepend: site.baseurl }}">
{{ page.title }}
</a>
</li>
</ol>
See this article for details and links to the implementation.
Thought I might throw this into the mix. This is based on Davelab6's example above, with a few improvements. The active class is set by the last entry in the loop - also contains permalinks for each crumb.
I haven't tested it with posts yet - but it should work. Let me know if any issues.
<ul class="breadcrumbs">
<li><a href="/">Home</a></li>
{% assign crumbs = page.url | split: '/' %}
{% assign crumbs_total = crumbs | size | minus: 1 %}
{% for crumb in crumbs offset: 1 %}
{% if forloop.index == crumbs_total %}
<li class="active">{{ crumb | replace:'-',' ' }}</li>
{% else %}
<li><a href="{% assign crumb_limit = forloop.index | plus: 1 %}{% for crumb in crumbs limit: crumb_limit %}{{ crumb | append: '/' }}{% endfor %}">{{ crumb | replace:'-',' ' }}</a>
{% endif %}
{% endfor %}
</ul>
I made the following breadcrumbs with liquid templates, its as elegant as I could manage :) It uses jQuery to set the active class on the li last li item.
<ul class="breadcrumb">
<li><a href="#"><i class="icon-home"></i>Home</a> </li>
{% assign crumbs = page.url | split: '/' %}
{% assign crumbstotal = crumbs | size %}
{% for crumb in crumbs offset:2 %}
{% unless crumb == 'index.html' %}
<li><span class="divider">»</span> {{ crumb | remove: '.html' }} </li>
{% endunless %}
{% endfor %}
</ul>
<script>$(".breadcrumb li").last().addClass('active');</script>
I managed to simply it even further. Here is what I'm using now:
{% assign crumbs = page.url | split: '/' %}
<ul class="lv-breadcrumbs">
<li><a href="/">Home</a></li>
{% for crumb in crumbs offset: 1 %}
{% if forloop.last %}
<li class="active">{{ crumb | replace:'-',' ' }}</li>
{% else %}
<li><a href="{% assign crumb_limit = forloop.index | plus: 1 %}{% for crumb in crumbs limit: crumb_limit %}{{ crumb | append: '/' }}{% endfor %}">{{ crumb | replace:'-',' ' }}</a></li>
{% endif %}
{% endfor %}
</ul>