2016-08-13 7 views
0

私のJekyllサイトにはいくつかのコレクションがあります。forloopで使用するJekyllのさまざまなコレクションの変数

{% 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上記の2番目のコードは機能しません。私はpage_orderとforloop.lengthのための空白だけを持っています。私は、それはpage.labelで対応するコレクションを見つけることができないと思います。 – jupiteror

答えて

0

site.testimonialsでコレクションにアクセスすると、コレクションのドキュメント配列が取得されます。

{{ site.testimonials | inspect }} 

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

site.collectionをループしながら、あなたがコレクションにアクセスすると、あなたは、コレクションのオブジェクト受け取る:によって

{% for node in collection reversed %} 

を:あなたのケースで

{% 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.docs reversed %} 
関連する問題