1
votes

I have a Jekyll site with a collection, colleges, that I want to sort and display by a custom rankings front matter attribute. Here is an example of items in the collection:

---
title: Example College
score: 88
rankings:
  - top accredited colleges
  - most affordable
---

...

---
title: Sample College
score: 75
rankings:
  - most affordable
  - best online programs
---

...

---
title: Example University
score: 75
rankings:
  - top accredited colleges
  - best online programs
---

I would like to display the different rankings as headings with the colleges listed under them like so:

top accredited colleges
- Example College
- Example University

most affordable
- Example College
- Sample College

best online programs
- Sample College
- Example University

I've tried using Liquid's group_by filter like so:

{% assign groups = site.colleges | group_by: "rankings" %}
{% for group in groups %}
    <h2>{{ group.name }}</h2>
    <ul>
        {% for college in group.items %}
            <li>{{ college.title }}</li>
        {% endfor %}
    </ul>
{% endfor %}

But this seems to group the rankings front matter by an exact match of the full array rather than separating out the different values it contains:

<h2>[top accredited colleges, most affordable]</h2>
<ul>
    <li>Example College</li>
</ul>

<h2>[most affordable, best online programs]</h2>
<ul>
    <li>Sample College</li>
</ul>

<!-- etc -->

How do I separate out the individual values from the rankings front matter array in the output?

1

1 Answers

1
votes

This can do the trick :

{% assign allRankings = site.colleges | map: "rankings" %}
{% assign rankingArray = "" | split:"" %}
{% for rankings in allRankings %}
  {% assign rankingArray = rankingArray | concat: rankings | uniq %}
{% endfor %}
{% for ranking in rankingArray %}
  <h2>{{ ranking | capitalize }}</h2>
  <ul>
  {% for college in site.colleges %}
    {% if college.rankings contains ranking %}
    <li>{{ college.title }}</li>
    {% endif %}
  {% endfor %}
  </ul>
{% endfor %}