Adam
Adam

Reputation: 113

Jekyll – Check if page belongs to a collection

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.

Upvotes: 2

Views: 1684

Answers (2)

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 %}

Upvotes: 5

David Jacquel
David Jacquel

Reputation: 52789

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

Upvotes: 3

Related Questions