given the following code in a jekyll template
{% assign resource = site.data.resources | where: "id", page.resource %}
that derives the following hash:
{
"id"=>"resource-1234",
"title"=>"testing",
"description"=>"Quis autem vel eum iure reprehenderit qui"
}
How do I user liquid to output the value of the title key? I have tried the following:
{{ resource }} # outputs the hash
{{ resource.title }} # nil
{{ resource["title"] }} # nil
In fact, the where
filter returns an array.
When you print this array with {{ resource }}
, it simply output all items one after an other. Here it print your hash, And this makes you think that resource
is a hash.
For debugging you can use {{ resource | inspect }}
that will return :
[{"id"=>"resource-1234", "title"=>"testing", "description"=>"Quis ..."}]
And here you see the brackets, and you know that resource
is an array.
In your case, you know that only one (or zero) resource is linked to your page. In order to get only the first resource hash, you can do :
{% assign resource = site.data.resources | where: "id", page.resource | first %}
Now {{ resource.title }}
returns testing
.
Cool isn't it ?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With