2016-08-24 46 views
0

我正在尝试创建导航到集合中上一页/下一页的左/右箭头链接。这是很容易实现的标准收取:如何分页排序的Jekyll集合?

{% if page.previous %} 
    <a href="{{ page.previous.url }}>Left arrow</a> 
{% endif %} 
{% if page.next %} 
    <a href="{{ page.next.url }}>Right arrow</a> 
{% endif %} 

我的问题是,这只是通过文件的顺序收集周期。我需要使用frontmatter变量(position)为页面指定特定的顺序。

我已经搜索了很多,但所有的信息与排序似乎适用于网站导航,而不是分页。看起来我需要在将已排序的集合分配给一个变量后使用循环液体。我正努力使page.previouspage.next变量在此循环中工作。

我已经尝试依次通过分类收集,只有当输出的文档的URL的当前页面匹配的导航箭头在文件中,但似乎并没有工作:

{% assign sorted_collection site.collection | sort: "position" %} 

{% for document in sorted_collection %} 

    {% if document.url == page.url %} 

    {% if document.previous %} 
     <a href="{{ document.previous.url }}>Left arrow</a> 
    {% endif %} 

    {% if document.next %} 
     <a href="{{ document.next.url }}>Right arrow</a> 
    {% endif %} 

    {% endif %} 

{% endfor %} 

上午我在我的液体语法中缺少一些东西,或者我在这里完全按照我的逻辑走错了路线?

我目前的黑客解决方案是用数字前缀文档文件名,以确保默认情况下它们的顺序是正确的。然而,当我将网站交给网站时,这是行不通的,因为我无法相信非技术性内容创作者保持文件名的顺序。

回答

0

我刚刚发现并测试了解决方案suggested here,它的工作原理!我认为诀窍在于捕获集合名称并将其作为字符串分配给变量。

有没有人有这样做的更好/较少详细的方式?

2

您链接的技术几乎是我在集合中使用的下一个/上一个。如果链接过时,这里有一个类似的片段可以保存收集项目对象,而不仅仅是URL:

{% if page.collection %} 
    {% assign collection = site[page.collection] | sort: 'position' %} 
    {% assign prev = collection | last %} 
    {% assign next = collection | first %} 

    {% for item in collection %} 
    {% if item.title == page.title %} 
     {% unless forloop.first %} 
     {% assign prev = iterator %} 
     {% endunless %} 

     {% unless forloop.last %} 
     {% assign next = collection[forloop.index] %} 
     {% endunless %} 
    {% endif %} 

    {% assign iterator = item %} 
    {% endfor %} 
{% endif %} 

{% if prev %} 
    <a href="{{ prev.url }}">{{ prev.title }}</a> 
{% endif %} 

{% if next %} 
    <a href="{{ next.url }}">{{ next.title }}</a> 
{% endif %}