Reputation: 135
I am using YAML/Jekyll/Liquid to generate a page and I have a file taxonomy.yml which contains "types" of items (this isn't the same as category) and properties of the items.
Here's an example:
fruits:
- apple:
color: red
size: medium
native: true
- grape:
color: green
size: small
native: false
Now I want to make use of these lists in 2 ways.
Types of Fruit
- Apple is red and medium size
- Grape is green and small size
This is what I have so far, but not outputting anything:
<ul>
{% for item in site.data.taxonomy.fruits %}
<li>??? is {{ item.color}} and size {{ item.size }}</li>
{% endfor %}
</ul>
---
title: The Pink Lady
fruit: apple
---
# All about the Pink Lady
Blah blah blah...
Then based on the fruit (apple in this case) I want to be able to put in the post page, something like this:
The Pink Lady
This article is about a medium fruit.
All about the Pink Lady
Blah blah blah...
To do this I have to go off to the taxonomy.yml, and look up the "apple" (from the front matter) against the "fruits" list, then read off the "size" attribute (medium in this case) to print to the post.
(Assume the values are unique, so there is only one 'apple' in the file and it will always have a 'size' specified)
This is what I have:
<h1>{{ page.title }}</h1>
<p>This article is about a {{ site.data.taxonomy.fruits[page.fruit].size }} fruit.</p>
I think I must have failed to understand some detail about working with hierarchies as I believe the idea is basically there but lacking some detail... please help!
I think my misunderstanding may be linked to the distinction between "list items" and "key-value pairs" in some way. I understand those in the abstract but struggling with applying them here.
Upvotes: 0
Views: 316
Reputation: 39668
You are probably misusing YAML sequences here. Your YAML:
fruits:
- apple:
color: red
size: medium
native: true
- grape:
color: green
size: small
native: false
says that fruits
is a list, where each list item is a mapping with a single key-value pair; the key being apple
or grape
, and the value being a mapping containing their properties. You probably want to do either of the following things:
fruits:
apple:
color: red
size: medium
native: true
grape:
color: green
size: small
native: false
Then you can iterate over the mapping, where item
will be a tuple of the key and the value:
<ul>
{% for item in site.data.taxonomy.fruits %}
<li>{{item[0]}} is {{item[1].color}} and size {{item[1].size }}</li>
{% endfor %}
</ul>
Maybe you do want fruits
to be a sequence. Then you should probably merge the names into the list of properties like so:
fruits:
- name: apple
color: red
size: medium
native: true
- name: grape
color: green
size: small
native: false
Now you can iterate over the sequence, and item
will directly contain the list of properties:
<ul>
{% for item in site.data.taxonomy.fruits %}
<li>{{item.name}} is {{ item.color}} and size {{ item.size }}</li>
{% endfor %}
</ul>
<ul>
{% for item in site.data.taxonomy.fruits %}
{% for kvpair in item %}
<li>{{kvpair[0]}} is {{kvpair[1].color}} and size {{kvpair[1].size }}</li>
{% endfor %}
{% endfor %}
</ul>
Upvotes: 1