RMeli
RMeli

Reputation: 188

How to select specific elements of a Jekyll collection

I am trying to select a few specific items from a collection in Jekyll. I managed to do so with the following code:

{% for paper in site.papers %}
{% if paper.paper-id == "Trott2010" %}
[{{ paper.title }}]({{ paper.url }})
{% endif %}
{% endfor %}

but is not at all elegant. Looking around i found this StackOverflow question and the answer seems exactly what I need:

{% assign paper = site.papers | where:"Trott2010", page.paper-id  | first %}

This works as expected if I only use it once per page. Unfortunately if I want to get more than one paper prom site.papers (assigning it to variables with different names), it does not work and I really don't understand way. If I use

{% assign paper1 = site.papers | where:"Trott2010", page.paper-id  | first %}
[{{ paper1.title }}]({{ paper1.url }})

{% assign paper2 = site.papers | where:"Scousa2013", page.paper-id  | first %}
[{{ paper2.title }}]({{ paper2.url }})

the output is exactly the same in the two instance.

Any help is appreciated.

Upvotes: 0

Views: 306

Answers (1)

where selects all the objects in an array where the key has the given value (array | where: "key", "value"). So in your case it should be:

{% assign paper1 = site.papers | where: "paper-id", "Trott2010" | first %}
[{{ paper1.title }}]({{ paper1.url }})

{% assign paper2 = site.papers | where:"paper-id", "Scousa2013" | first %}
[{{ paper2.title }}]({{ paper2.url }})

Upvotes: 2

Related Questions