Jekyll 中不同集合的一个变量,用于 for 循环
One variable for different collections in Jekyll to use in forloop
我的 Jekyll 站点上有几个集合。我已将 post 导航添加到其中一个集合中,在每个 post 页面上显示一个计数器:
{% 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 中的所有集合传递一个变量,以便在 post 导航中的 forloop 中使用?
当您使用 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 %}
我的 Jekyll 站点上有几个集合。我已将 post 导航添加到其中一个集合中,在每个 post 页面上显示一个计数器:
{% 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 中的所有集合传递一个变量,以便在 post 导航中的 forloop 中使用?
当您使用 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 %}