Tom Johnson
Tom Johnson

Reputation: 759

How to loop through all files in Jekyll's _data folder?

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?

Upvotes: 4

Views: 3810

Answers (4)

tomdinh
tomdinh

Reputation: 198

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

Upvotes: 1

eQ19
eQ19

Reputation: 10691

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 %}

Upvotes: 0

jme
jme

Reputation: 81

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.

Upvotes: 6

rocambille
rocambille

Reputation: 15956

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 %}

Upvotes: 0

Related Questions