Vladimir Kiva
Vladimir Kiva

Reputation: 45

Jekill sort by a property specified in the variable

I'm creating a template using Jekill. Is it possible to sort items by a property specified in the variable like this:

<!-- Collection identity -->
{% assign collection_id = page.id %}

<!-- Select pages and sort them by a value provided for the collection -->
{% assign product_pages = (site.pages | sort: [collection_id]) %}

The sorting doesn't work (compile error).

The task is to have some pages that define categories with headers like this one:

---
layout: collection
id: sale
...
---

Then some pages could be in one or more collections and so as to define their sort order inside each collectino the page would have a header like this:

---
layout: item
sale: 100
news: 30
general: 1000
...
---

Sale, News, General etc have the same template that should be automatically generated and they could have sorted collection.

Upvotes: 0

Views: 48

Answers (1)

marcanuy
marcanuy

Reputation: 23962

Yes, it is possible to sort items by a property, but pages do not have an id property, you can however, sort them by another property, e.g.: name.

Unsorted:

{% for p in site.pages %}
{{p.name}}
{% endfor %}

Sorted by page.name:

{% assign product_pages = site.pages | sort: 'name' %}

{% for p in product_pages %}
{{p.name}}
{% endfor %}

Update

To sort with a custom id property added to pages:

{% assign product_pages = site.pages | sort: 'id' %}

Upvotes: 1

Related Questions