Trying to parse data file, Hugo is silent

I’m trying to parse this data file, called projects.yml

- name: My awesome project
  type: Corporate website
  work: Wordpress - Front and backend development.
  text: Yoga is fun!
  client: Lemon Co. Ltd
  url: "http://www.google.com"
- name: My super duper project
  type: Corporate website
  work: Wordpress - Graphics, Front and backend development.
  text: I love cake.
  client: SomethingSomething
  url: "http://www.facebook.com"

Inside a projects.html template, I run this loop:

{{ range .Site.Data.projects }}
        {{ .name }}
{{ end }}

For some reason, I cannot get Hugo to output any data from this file. I must be doing something wrong, but I can’t figure out what. I’ve checked the yml file, it’s valid, so it must be my range?

Can anyone point me to the right direction?

First of all projects.yml needs to be under/data/projects/

If that didn’t fix your issue you need to tell us where is projects.html located and where have you put its template.

Try this. Not tested.

{{ range $i, $w := .Site.Data.projects }}
        {{ $w.name }}
{{ end }}

Upd. Not worked also.

My code will work with this data:

1:
  name: My awesome project
  type: Corporate website
  work: Wordpress - Front and backend development.
  text: Yoga is fun!
  client: Lemon Co. Ltd
  url: "http://www.google.com"
2:
  name: My super duper project
  type: Corporate website
  work: Wordpress - Graphics, Front and backend development.
  text: I love cake.
  client: SomethingSomething
  url: "http://www.facebook.com"
1 Like

This is my structure:

/data/projects.yml
/content/projects/_index.md
/layouts/section/projects.html

_index.md contains only one variable inside the Front Matter called title (which is used as H1 in the template).

I’ve tried moving the projects.yml file in /data/projects but I still can’t parse the file. I’ve also tried your suggestion @Mikhail but still nothing on my end. :thinking:

Your template structure is correct, the problem is in the data structure inside the yaml-file.

It might have something to do with the way your YAML is setup. It looks like you may need to add assign your map to a property, so try something like:

projects:
    - name: My awesome project
      type: Corporate website
      work: Wordpress - Front and backend development.
      text: Yoga is fun!
      client: Lemon Co. Ltd
      url: "http://www.google.com"
    - name: My super duper project
      type: Corporate website
      work: Wordpress - Graphics, Front and backend development.
      text: I love cake.
      client: SomethingSomething
      url: "http://www.facebook.com"

Then in your template:

{{ range .Site.Data.projects.projects }}
  {{ .name }}
{{ end }}
2 Likes

The question of how to range an array in the root of the yaml-file. I do not know the answer. I showed above how to do differently.

Thanks a lot @Mikhail and @HeyOzRamos, both your solutions work.

To sum up, if anyone is stumbling upon this thread, here’s how my data is structured:

/data/projects.yml
/content/projects/_index.md
/layouts/section/projects.html

Solution1:

<!-- /layouts/section/projects.html  -->
{{ range .Site.Data.projects }}
    {{ .name }}
{{ end }}

Works with this projects.yml file:

# /data/projects.yml
1:
  name: My awesome project
  type: Corporate website
2:
  name: My super duper project
  type: Corporate website

Solution 2:

<!-- /layouts/section/projects.html -->
{{ range .Site.Data.projects.projects }}
    {{ .name }}
{{ end }}

Works with this projects.yml file:

# /data/projects.yml
projects:
    - name: My awesome project
      type: Corporate website
    - name: My super duper project
      type: Corporate website

It’s a bummer that it wouldn’t work with my original file, as it’s a valid yml file and Hugo gave me no error, but I’m glad I’ve found a solution thanks to you guys.

4 Likes