Jekyll – Check if page belongs to a collection

拈花ヽ惹草 提交于 2020-01-15 10:16:10

问题


Is there a way to check if a current page belongs to a collection? If so, can I check what’s the label of the collection?

Background story: What I’m trying to achieve is to display a different layout for all pages that belong to a collection. With this new layout I want to display a side navigation (some sort of „See also”) with listing of all pages in the collection that the current site belongs to.

In case my question turns out to be dumb – forgive me, I’m new to jekyll and I really tried to find out the answer by myself.


回答1:


page.collection returns the label of the collection to which the document belongs. So I guess you want to do something like:

{% if page.collection == 'COLLECTION_LABEL' %}
NEW LAYOUT
{% else %}
OLD LAYOUT
{% endif %}

To access the pages in a collection, you can use the label with site.COLLECTION_LABEL or site[COLLECTION_LABEL] (you need [] if it is a variable). Something like:

{% for page in site[page.collection] %}
<a href="{{ page.url }}">{{ page.title }}</a>
{% endfor %}



回答2:


Any collection document has a page.collection variable which is the collection label.

But, if you want to associate a specific layout to a collection, you can use default configuration :

defaults:
  -
    scope:
      type: mycollection
    values:
      layout: a_custom_layout
      anyvar: a_custom_var


来源:https://stackoverflow.com/questions/53923035/jekyll-check-if-page-belongs-to-a-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!