Liquid for loop 在现场工作但不在本地工作

Liquid for loop working in live site but not locally

我刚开始开发新的 OSX 10.11.2。我毫不费力地安装了 Jekyll 和所有依赖项,然后在本地克隆了这个 repo:https://github.com/jseldess/jseldess.github.io

当我从存储库 (http://jseldess.github.io/) 查看实时站点时,默认布局中的 Liquid 循环成功地在侧边栏中生成了一个页面树。但是当我在本地提供网站时,侧边栏是空的。我不明白为什么会这样。有任何想法吗?

这是循环:

{% 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 %}

谢谢!

Github 页面正在使用 Jekyll 2.4,而您正在本地使用 Jekyll 3.x。

区别在于 Jekyll Collection 迭代器在 2 和 3 中的行为不同。

杰基尔 2 return:Array [ String, Hash ] 哲基尔 3 return : Hash

所以你的 {% assign collection = item[1] %} 在 Jekyll 3 上失败了。

Jekyll 2/3 兼容代码可以是:

{% 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 %}

为了与 Github 页面设置同步,您可以使用捆绑器和 Gemfile。 See Github page documentation here.