2016-08-13 45 views
0

我的Jekyll站點上有幾個集合。我已經添加後導航到每個崗位頁面上顯示計數器的收藏品之一:Jekyll中的不同集合的一個變量用於forloop

{% assign testimonials = site.testimonials %} 
{% assign page_order = 1 %} 
{% for node in testimonials reversed %} 
    {% if node.url == page.url %} 
    {{ page_order }} from {{ forloop.length }} 
    {% else %} 
    {% assign page_order = page_order | plus: 1 %} 
    {% endif %} 
{% endfor %} 

我想作此代碼的工作不僅對site.testimonials,但對於其他收藏品也。我試圖通過一個像這樣的集合變量:

{% capture label %}{{ page.collection }}{% endcapture %} 
{% assign collection = site.collections | where: "label",label | first %} 
{% for node in collection reversed %} 
    {% if node.url == page.url %} 
    {{ page_order }} from {{ forloop.length }} 
    {% else %} 
    {% assign page_order = page_order | plus: 1 %} 
    {% endif %} 
{% endfor %} 

但它不起作用。有沒有什麼辦法讓Jekyll中的所有集合的變量都可以在後導航中用於forloop?

+0

什麼不起作用? – wasthishelpful

+0

@wasthishelpful上面的第二個代碼不起作用。我只有page_order和forloop.length空的空格。我想,它無法找到與page.label相應的集合。 – jupiteror

回答

0

當您使用site.testimonials訪問集合時,將獲得集合的文檔數組。

{{ site.testimonials | inspect }} 

# output >> [#<Jekyll::Document ...>, #<Jekyll::Document ...>, ...] 

當你訪問一個集合,而遍歷site.collection,您會收到收集的對象:

{% assign collection = site.collections | where: "label",page.collection | first %} 
{{ collection | inspect }} 
# output >> { "output": true, "label": "collectionLabel", 
       "docs": [ doc1, docs2, ... ], "files": [], 
       "directory": "/path/to/collection", 
       "relative_directory": "_colectionDirectory" } 

在你的情況,你只需要更換:

{% for node in collection reversed %} 

通過:

{% for node in collection.docs reversed %} 
相關問題