Search code examples
githubsplitbreadcrumbsliquid

Attempting to write breadcrumbs for octopress and failing


Here is my attempt so far. I realise that there may be other ways to do this.

But why is my call to split failing?

{% capture path %}
  {{ page.url | remove: ".html" | remove_first:"/" }}
{% endcapture %}
{% capture breadcrumbs %}
  {{ path | split:'/' }}
{% endcapture %}
{% for crumb in breadcrumbs %}
  {{ crumb }} » 
{% endfor %} 

I am uploading this to github and just getting nothing.


Solution

  • I've never been able to get split to work properly either, but breadcrumbs are still possible. The following is adapted from my code in that answer (note the section that should be modified in the if statement and that this is the readable version and doesn't work precisely as expected).

    {% capture url_parts %} {{ page.url | remove: "/index.html" | replace:'/'," " }}{% endcapture %}
    {% capture num_parts %}{{ url_parts | number_of_words }}{% endcapture %}
    {% assign previous="" %}
    {% if num_parts == "0" %}
      <Handle being at the top of the site (i.e. "site.com/") here>
    {% else %}
     {% for unused in page.content limit:num_parts %}
      {% capture first_word %}{{ url_parts | truncatewords:1 | remove:"&hellip;"}}{% endcapture %}
      {{ first_word }} &#187;
      {% capture url_parts %}{{ url_parts | remove_first:first_word }}{% endcapture %}
     {% endfor %}
    {% endif %}
    

    If the user is at /a/b/c.html this will print a » b » c », if they are at /a/b/ (or equivalently /a/b/index.html) it will just print a » b ».

    At least, it will be close to that: for a Markdown file there are too many newlines between each time first_word is printed, so they are considered separate paragraphs and the output will be (this isn't a problem in an HTML file, but then more tags are needed to make it work properly):

    a »
    
    b »
    
    c »
    

    This can be solved by putting the whole for loop on one line (this is the code that should be used):

    {% capture url_parts %} {{ page.url | remove: "/index.html" | replace:'/'," " }}{% endcapture %}
    {% capture num_parts %}{{ url_parts | number_of_words }}{% endcapture %}
    {% assign previous="" %}
    {% if num_parts == "0" %}
      <Handle being at the top of the site (i.e. "site.com/") here>
    {% else %}
     {% for unused in page.content limit:num_parts %}{% capture first_word %}{{ url_parts | truncatewords:1 | remove:"..."}}{% endcapture %}{{ first_word }} &#187;{% capture url_parts %}{{ url_parts | remove_first:first_word }}{% endcapture %}{% endfor %}
    {% endif %}
    

    (NB. the page.content in the for loop is just to give something to iterate over, the magic is done by the limit:num_parts. However, this means that if page.content has fewer paragraphs than num_parts not all breadcrumbs will appear, if this is likely one might define a site variable in _config.yml like breadcrumb_list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] and use site.breadcrumb_list as the placeholder instead of page.content. (Lifted from my other answer.))

    Here is an example (it doesn't use precisely the same code as above, but it's just a few little modifications, and it is in an HTML file, so the problem with new lines creating paragraphs is not there).