Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll display collection by category

Tags:

jekyll

I'm having trouble figuring out how to sort the content of a collection by category. I've found a very similar question on how to sort posts by category but it doesn't seem to work with collections Jekyll display posts by category.

Let's say I have a collection called 'cars' with two categories: 'old' and 'new'. How can I display only cars with the category 'old'?

like image 844
Franz Avatar asked Jan 22 '15 23:01

Franz


1 Answers

You can sort, group or filter collection like any other object like page or posts.

_my_collection/mustang.md

---
category: 'old'
abbrev: tang
---
mustang content

_my_collection/corvette.md

---
category: 'new'
abbrev: vet
---
corvette content

Filtering

{% assign newcars = site.my_collection | where: "category", "new" %}

Sorting

{% assign allcarssorted = site.my_collection | sort: "category" %}

Grouping

{% assign groups = site.my_collection | group_by: "category" | sort: "name" %}

This groups your cars and then sort groups by name that is the category.

{% for group in groups %}
    {{ group.name }}
    {% for item in group.items %}
        {{item.abbrev}}
    {%endfor%}
{%endfor%}
like image 142
David Jacquel Avatar answered Oct 22 '22 00:10

David Jacquel