# Iterating through a list of data files dynamically

**URL:** <https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728>\
**Category:** support\
**Created:** [September 16, 2015, 5:41am UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728 "2015-09-16T05:41:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [September 16, 2015, 5:41am UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728/1 "2015-09-16T05:41:24Z")

</div>

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:

```auto
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:

```auto
{{range $person := .Params.guests}}
    {{.Site.Data.guests.$person.Name}}
{{ 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?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [September 16, 2015, 10:12am UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728/2 "2015-09-16T10:12:41Z")

</div>

> [@mattstratton](#):
>
> {{.Site.Data.guests.$person.Name}}

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

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

```

---

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [September 16, 2015, 12:37pm UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728/3 "2015-09-16T12:37:29Z")

</div>

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

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

```

which resulted in this:

```auto
ERROR: 2015/09/16 Error while rendering page episode/episode/chatops.md: 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.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [September 16, 2015, 1:07pm UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728/4 "2015-09-16T13:07:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [September 16, 2015, 1:19pm UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728/5 "2015-09-16T13:19:40Z")

</div>

I added in the suggested code into another branch, which can be located here [https://github.com/arresteddevops/ado-hugo/tree/dot-issue](https://github.com/arresteddevops/ado-hugo/tree/dot-issue)

The relevant files are:

```auto
themes/ado/episode/single.html
data/guests/jhand.yml

```

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

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [September 16, 2015, 2:43pm UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728/6 "2015-09-16T14:43:59Z")

</div>

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

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

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

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

```

This one should solve it:

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

```

---

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [September 16, 2015, 6:17pm UTC](https://discourse.gohugo.io/t/iterating-through-a-list-of-data-files-dynamically/1728/7 "2015-09-16T18:17:29Z")

</div>

That did it! Thank you SO MUCH.
