Search code examples
jekyllliquidosx-elcapitan

Liquid for loop working in live site but not locally


I just started working on a new OSX 10.11.2. I installed Jekyll and all dependencies without trouble and then cloned this repo locally: https://github.com/jseldess/jseldess.github.io

When I view the live site from the repository (http://jseldess.github.io/), the Liquid loop in the default layout successfully generates a page tree in the sidebar. But when I serve up the site locally, that sidebar is empty. I can't figure out why that's happening. Any ideas?

Here's the loop:

{% for item in site.collections %}
    {% assign collection = item[1] %}
    {% if section %}
        {% if collection.output %}
            {% assign next = collection %}
            {% break %}
        {% endif %}
    {% elsif url contains collection.active_prefix %}
        {% assign section = collection.title %}
        {% assign items = collection.docs | sort: 'order' %}
    {% elsif collection.output %}
        {% assign previous = collection %}
    {% endif %}
{% endfor %}

Thanks!


Solution

  • Github pages is using Jekyll 2.4 and you're using Jekyll 3.x locally.

    The difference is that Jekyll Collection iterator behave differently in 2 and 3.

    Jekyll 2 return : Array [ String, Hash ] Jekyll 3 return : Hash

    So your {% assign collection = item[1] %} is failing with Jekyll 3.

    A Jekyll 2/3 compatible code can be :

    {% for item in site.collections %}
        {% assign itemLength = item | size %}
        {% comment %}Jekyll 2 returns an array with length = 2{% endcomment %}
        {% if itemLength == 2 %}
            {% assign collection = item[1] %}
        {% else %}{% comment %}Jekyll 3 returns a hash with length > 2{% endcomment %}
            {% assign collection = item %}
        {% endif %}
        {% if collection.output %}
            {% assign parts = url | split: "/" %}
            <li class="nav-item top-level {% if parts[1] == collection.active_prefix %}current{% endif %}">
                {% assign items = collection.docs | sort: 'order' %}
                <a href="{{ items.first.url }}">{{ collection.title }}</a>
                {% include secondary-nav-items.html items=items %}
            </li>
        {% endif %}
    {% endfor %}
    

    In order to work in sync with Github pages setup, you can work with bundler and a a Gemfile. See Github page documentation here.