Iterating through a list of data files dynamically

Another newbie question.

For our podcast, each episode has one or more guests. As you might surmise, sometimes a guest appears on more than one episode. I would like to create a data file for each guest that contains the pertinent information about the guest (Name, bio, twitter handle, web url, etc), and then have each episode have frontmatter that lists the guests for that episode.

The problem I’m running into is pulling in the list of the guests and then passing that into a call to the data file.

In my frontmatter, I have this:

Guests = ["jhand", "srosenbaum"]
Each guest has a .yml file in /data/guests that looks like this:

Name: jhand
"Short Description": "He is a **jolly good** fellow."
"Twitter": "jasonhand"

Then, what I want to be able to do is something like this:

{{range $person := .Params.guests}}
{{ end }}

I seem to be missing something, as Hugo doesn’t want me to insert a variable into the middle of the path to the data. Ideas?

1 Like

Yes, that doesn’t work, but this should:

{{ (index .Site.Data.guests $person).Name}}

Thanks @bep - unfortunately that didn’t work. I used this code:

{{ range $person :=.Params.guests }}
  {{ $person }}<br />
  {{ (index .Site.Data.guests $person).Name}}
{{ end }}

which resulted in this:

ERROR: 2015/09/16 Error while rendering page episode/episode/ template: theme/episode/single.html:17:17: executing "theme/episode/single.html" at <.Site.Data.guests>: can't evaluate field Site in type string

I tried modifying to {{ (index $.Site.Data.guests $person).Name}} thinking it was a dot issue, but that caused Hugo to crash, hard.

Well, it is definitely a “dot issue”, but it is impossible to see where the dot is without any context.

I added in the suggested code into another branch, which can be located here

The relevant files are:


I really appreciate your assistance. Fixing this will jumpstart me. I’ve already learned a ton.

OK, it was a “dot error” and another.

You get a null reference error when the data element doesn’t exist:

{{ (index $.Site.Data.guests "jhand2").Name }} ==> crash

{{ (index $.Site.Data.guests "jhand").Name }} ==> works

This one should solve it:

{{ if isset $.Site.Data.guests $person }}{{ (index $.Site.Data.guests $person).Name }}{{ end }}

That did it! Thank you SO MUCH.