0

I have a problem very similar to this one, but unfortunately, the solution doesn't work for me. So I'm posting another question.

I'm making a website with Jekyll and would like to print the information of a yml file on a page or post. Here is a dummy yml file:

- Master student:
  - Dummy name1:
    - email: blabal@bla.com
    - room: 1
    - tel: 12345678


- Bachelor student:
  - Dummy name:
    - email: blabal@bla.com
    - room: 12
    - tel: 0987654

- Guest:
  - Bird:
    - email: blabal@bla.com
    - room: 10856
    - tel: 71864270

This file is placed in a newly made _data directory, under the name people.yml. Then, I made a new post on a freshly made site with the minimal theme, which goes as follows:

---
layout: post
title:  "Test"
date:   2023-01-12 23:42:09 +0100
---


{% assign people = site.data.people | read_yaml %}
<!-- {{ people }} --> // this tests if the files is loaded or not


{% for role in people %}
  {% assign role_data = role[1] %}
  {{ role_data[0] }} has the following role: {{ role[0] }}.
{% endfor %}

Once I generate the site, I expect the following text on the post (except maybe for some linebreaks):

Dummy name1 has the following role: Master student
Dummy name has the following role: Bachelor student
Bird has the following role: Guest

Instead, I get this:

has the following role: .

has the following role: .

has the following role: .

that puzzles me. I suspect the reason is the way I access the item values. But can't figure it out. Removing the read_yml also seems to have no effect.

arash
  • 161
  • 13

1 Answers1

2

You were on the right track, but didn't do enough un-nesting of your hashes. The data structure is a bit unfortunate; maybe when you look at the equivalent JSON, it's more obvious:

[
  {
    "Master student": [
      {
        "Dummy name1": [
          {
            "email": "blabal@bla.com"
          },
          {
            "room": 1
          },
          {
            "tel": 12345678
          }
        ]
      }
    ]
  },
  {
    "Bachelor student": [
      {
        "Dummy name": [
          {
            "email": "blabal@bla.com"
          },
          {
            "room": 12
          },
          {
            "tel": 987654
          }
        ]
      }
    ]
  },
  {
    "Guest": [
      {
        "Bird": [
          {
            "email": "blabal@bla.com"
          },
          {
            "room": 10856
          },
          {
            "tel": 71864270
          }
        ]
      }
    ]
  }
]

So, each array element is a hash (object), and to get key and value, you have to iterate over the hashes; the values themselves are again arrays of hashes. This snippet produces your desired output:

{% assign people = site.data.people %}

{% for person in people %}
  {%- for person_hash in person %}
    {%- assign role = person_hash[0] %}
    {%- for role_hash in person_hash[1][0] %}
      {{- role_hash[0] }} has the following role: {{ role }}.
    {% endfor %}
  {% endfor %}
{% endfor %}

I would recommend you re-structure your data, to something like this:

- role: Master student
  name: Dummy name1
  email: blabal@bla.com
  room: 1
  tel: 12345678
- role: Bachelor student
  name: Dummy name
  email: blabal@bla.com
  room: 12
  tel: 0987654
- role: Guest
  name: Bird
  email: blabal@bla.com
  room: 10856
  tel: 71864270

which corresponds to this JSON:

[
  {
    "role": "Master student",
    "name": "Dummy name1",
    "email": "blabal@bla.com",
    "room": 1,
    "tel": 12345678
  },
  {
    "role": "Bachelor student",
    "name": "Dummy name",
    "email": "blabal@bla.com",
    "room": 12,
    "tel": 987654
  },
  {
    "role": "Guest",
    "name": "Bird",
    "email": "blabal@bla.com",
    "room": 10856,
    "tel": 71864270
  }
]

Then, iterating over it becomes as simple as this:

{% for person in site.data.people %}
  {{ person.name }} has the following role: {{ person.role }}
{% endfor %}
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116