4

How can I loop through every file in my _data folder in Jekyll?

Currently I have a list of files in a file called sidebarlist.yml like this:

- file1
- file2
- file3

In order to loop through all of these files, I use this code:

{% for sidebar in site.data.sidebarlist %}
{% for entry in site.data.sidebars[sidebar].entries %}
...
{% endfor %}
{% endfor %}

I would like to avoid using sidebarlist.yml and just iterate through all files within _data automatically. Can I do this?

rocambille
  • 15,398
  • 12
  • 50
  • 68
Tom Johnson
  • 729
  • 8
  • 17

4 Answers4

6

Nesting loops allows you to loop through the contents of _data files.

When I did this I used a subdirectory, since I didn't want to loop through every data file, and I think that applies to many use cases. It also keeps my _data directory a little tidier.

My _data directory looks like this:

_data/
  navigation.yml
  news.yml
  people/
    advisors.yml
    board.yml
    staff.yml

Each of the files within people/ uses a structure like this:

- name: Anne Smith
  role: Role A
  url: mysite.com
- name: Joe Shmoe
  role: Role B
  url: mysite.org

And on the page where I'm looping through each of these data files:

{% for people_hash in site.data.people %}
{% assign people = people_hash[1] %}

  {% for person in people %}

    <li>{{ person.name }}, {{ person.role }}</li>

  {% endfor %}

{% endfor %}

This results in:

<li>Anne Smith, Role A</li>
<li>Joe Shmoe, Role B</li>

It's very similar to what you've already done, but eliminates the need for that extra yaml file.


Note the use of people_hash[1] - this is what is targeting the appropriate values within the array.

If instead you do:

{% for people_hash in site.data.people %}
{% assign people = people_hash[1] %}

    <pre>{{ people }}</pre>

{% endfor %}

You'll get the array of values that is returned, which should help you debug your template.

jme
  • 81
  • 1
  • 6
  • Great answer; assuming OP wants to iterate through a flat list of yaml files, this solution works just fine. – dancow Feb 20 '20 at 00:26
1

I have read your question title, and I will answer your last question:

You can't loop through files you keep in _data folder. According to Jekyll Variable doc and Jekyll Directory structure all the file in _data with supported extension .yml .yaml .csv .jsonby default will be loaded in site.data like @wasthishelpfull's answered and you access it via {{site.data.*filename.data*}} and loop though like this answer

If you wanna loop through files, create a folder (no underscore) serve it as static files, and use jquery.get() for the data in the file.

Or change _data to data in _config.yml by adding data_source: data and access at a url endpoint /data see this post for more

Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
tomdinh
  • 178
  • 8
0

According to the documentation, jekyll will load YAML resources (.yml, .yaml, .json, and .csv files) directly into site.data. If your files use one of these formats, you can do:

{% for data in site.data %}
    ...
{% endfor %}
rocambille
  • 15,398
  • 12
  • 50
  • 68
  • Thanks, while this does load the data files, it doesn't quite work for what I'm trying to accomplish. Maybe my use case is more complicated, since I'm trying to go into a specific yaml level within each of the files. – Tom Johnson Sep 19 '16 at 20:08
  • I wasn't sure to understand what you're trying to accomplish, so I didn't elaborate my answer. Feel free to precise your use case :) – rocambille Sep 19 '16 at 21:08
0

I assume you need to access jekyll site.data in a way of looping multi levels object:

{% assign my_data = site.data %}
{% assign my_level = "sidebarlist.sidebars.sidebar" | split: "." %}

{% for level in my_level %}
    {% assign my_data = my_data[level[i]] %}
    {% for data in my_data %}
        {{ data }} : {{ my_data[data] }}
    {% endfor %}
{% endfor %}

eQ19
  • 9,880
  • 3
  • 65
  • 77