Issue in looping through data files in Jekyll

#1

Hi,
I’m facing the issue of looping through the following simple yml file:

- name: Air
  id: air
  page-title: page1-title
  description: Decs_1

  group:
    - name: Grp-Name1
      id: Grp-name1-id
      page-title: Grp-name1-title
  group:
    - name: Grp-Name2
      id: Grp-name2-id
      page-title: Grp-name1-title
  group:
    - name: Grp-Name3
      id: Grp-name3-id
      page-title: Grp-name3-title

Here is the liquid code:
{% for niche in site.data.navig %}
Niche ID = {{ niche.id }}
{% for group in niche.group %}
group name = {{ group.name }}
{% endfor %}
{% endfor %}

Here is the result:

Niche ID = air
group name = Grp-Name3

As can be seen, only the last group in the hierarchy is returned (the last element in the returned array). Seems Jekyll does not return proper array. Note that the code is a typical code from all Jekyll tutorials (which neglect this issue assuming only one group is allowed under niche) – I need to have multiple “group” under “niche”
I would be grateful if someone come up with a solution (either the yml file or the liquid code or both)

Thanks

Behrouz

0 Likes

#2

I don’t think you can do the group like that. I think you need to do it like this:

 group:
    - name: Grp-Name1
      id: Grp-name1-id
      page-title: Grp-name1-title
    - name: Grp-Name2
      id: Grp-name2-id
      page-title: Grp-name1-title
    - name: Grp-Name3
      id: Grp-name3-id
      page-title: Grp-name3-title
0 Likes

#3

Thanks. But I need the group entries be under Air and all of them be available in the loop. Actually, I need a hierarchical structure.

0 Likes

#4

did my example not work? I think it would do what you want bu have not tested it. Looks like I have the sub items indented too far.

lots of good examples here:

0 Likes

#5

Hi rdyar, Your example needs its own code since it has different structure than mines. What I need a hierarchical structure (under end -name entry) not a flat structure. – Regards.

0 Likes

#6

I guess I don’t understand what you want. I just tried:

- name: Air
  id: air
  page-title: page1-title
  description: Decs_1
  group:
  - name: Grp-Name1
    id: Grp-name1-id
    page-title: Grp-name1-title
  - name: Grp-Name2
    id: Grp-name2-id
    page-title: Grp-name1-title
  - name: Grp-Name3
    id: Grp-name3-id
    page-title: Grp-name3-title

- name: Air2
  id: air2
  page-title: page1-title2
  description: Decs_12
  group:
  - name: Grp-Name12
    id: Grp-name1-id2
    page-title: Grp-name1-title2
  - name: Grp-Name22
    id: Grp-name2-id
    page-title: Grp-name1-title2
  - name: Grp-Name32
    id: Grp-name3-id2
    page-title: Grp-name3-title2
{% for niche in site.data.sections %}
Niche ID = {{ niche.id }} <br>
{% for group in niche.group %}
group name = {{ group.name }}<br>
{% endfor %}
<hr>
{% endfor %}

and got:

Niche ID = air 
group name = Grp-Name1
group name = Grp-Name2
group name = Grp-Name3
Niche ID = air2 
group name = Grp-Name12
group name = Grp-Name22
group name = Grp-Name32
0 Likes

#7

note that my data file name was different than yours so that needs to be changed.

0 Likes

#8

Thanks. Your yml and code work perfect. However, as I mentioned, I need more that on group under Alir (as the yml below). The code just pick up the last group under Air and misses the first one (or any other one above the last one) – it returns only “Grp-Name-10000” which is the last one:

  • name: Air
    id: air
    page-title: page1-title
    description: Decs_1
    group:

    • name: Grp-Name1
      id: Grp-name1-id
      page-title: Grp-name1-title
    • name: Grp-Name2
      id: Grp-name2-id
      page-title: Grp-name1-title
    • name: Grp-Name3
      id: Grp-name3-id
      page-title: Grp-name3-title

    group:

    • name: Grp-Name-10000
      id: Grp-name1-id-10000
      page-title: Grp-name1-title-10000
    • name: Grp-Name2-10000
      id: Grp-name2-id-10000
      page-title: Grp-name1-title-10000
    • name: Grp-Name3-10000
      id: Grp-name3-id-10000
      page-title: Grp-name3-title-10000
  • name: Air2
    id: air2
    page-title: page1-title2
    description: Decs_12
    group:

    • name: Grp-Name12
      id: Grp-name1-id2
      page-title: Grp-name1-title2
    • name: Grp-Name22
      id: Grp-name2-id
      page-title: Grp-name1-title2
    • name: Grp-Name32
      id: Grp-name3-id2
      page-title: Grp-name3-title2
0 Likes

#9

then you would need to name the 2nd group something else. I don’t think you can have more than one key with the same name, that doesn’t make sense.

If they are all groups, why are they not in the first group?

Imagine yourself as the computer, how would you be able to keep track of 2 things that have the same name but then consider them as different things? If you told me last week your name is paul, and then today you tell me your name is bob, tomorrow I am going to think your name is bob. You can only have 1 name. The data can only have 1 key.

That assumes I am understanding you correctly, which I may not be.

0 Likes

#10

Thanks for your comment. It makes sense. As a matter of fact, I understand I need to create a collection of “groups” (one collection per each level of hierarchy). So I’m building the whole structure on this concept.
Regards, Behrouz

0 Likes