0

I'm trying to define the sections of a page in the frontmatter of my Jekyll post like so:

---
title: Foobar
sections:
- First
- Second
- Third
---

This works fine, but I would now need to have some sections with nested items; no matter how I try, Jekyll is either not happy about any nested objects or outputs the nested "text", e.g. this:


---
title: Foobar
sections:
- First
- Second:
    - Nested one
    - Nested two
- Third
---

... will interpret "- Second: - Nested one - Nested two" as one list item.

I'm using those sections to generate inline anchor links in the page main navigation. How can I set up a nested list in the frontmatter for this?

kontur
  • 4,934
  • 2
  • 36
  • 62

1 Answers1

0

Not 100% sure what you have tried but Jekyll can be quite tricky at times. The frontmatter is an array, the nested list is a hash. To make results visible I use {{ variable | inspect }}.

Showing the entire front matter:

Code: {{ page.sections | inspect }}
Result: ["First", {"Second"=>["Nested one", "Nested two"]}, "Third"]

Accessing the first index (nested part):

Code: {{ page.sections[1] | inspect }}
Result: {"Second"=>["Nested one", "Nested two"]}

Accessing the key and the index for the value:

Code: {{ page.sections[1]["Second"][0] | inspect }}
Result: Nested one

You could also use a loop:

{% for item in page.sections %}
  {% if item["Second"] %}
    {{ item["Second"][0] }}
    {{ item["Second"][1] }}
  {% else %}
    {{ item }}
  {% endif %}
{% endfor %}
Christian
  • 4,902
  • 4
  • 24
  • 42