Using Jekyll 3.8.5 (for Github pages).
I have set up a collection _resources, which contains files like this:
---
title: Resource 1
categories:
- title: cat3
level: 1
- title: cat2
level: 2
---
I am using a simple Liquid for loop iterate through the categories and their data:
<ul>
{% for category in resource.categories %}
<li> Category: {{ category.title }} | Level: {{ category.level }}</li>
{% endfor %}
</ul>
The expected result should be:
<ul>
<li> Category: cat3 | Level: 1</li>
<li> Category: cat2 | Level: 2</li>
</ul>
Instead, I am getting extra <li>
elements:
<ul>
<li> Category: cat3 | Level: 1</li>
<li> Category: cat2 | Level: 2</li>
<li> Category: | Level: </li>
<li> Category: | Level: </li>
</ul>
I checked using {{ categories.size }}
and it returned 4
So, I'm sure I'm doing something wrong with my YAML formatting, for Jekyll to think there are two extra categories.
I've tried removing the hyphens in the YAML, but Jekyll throws an error:
no implicit conversion of Hash into Array
Any help very gratefully received.
Thanks,
Mark
tag
, tags
, category
and categories
have a special meaning for documents in a collection (posts included).
Jekyll expects a document's tag
and category
attributes to be simple strings and
attributes tags
and categories
to be simple arrays.
You can try swapping the categories
key for something else say foobars
..
Then if your updated Liquid loop renders the expected markup, you're all set. Replace foobars
with a more appropriate label.
If the issue still occurs, there's something wrong in your layout / include.