# \[SOLVED\] Loop through data file with keys

**URL:** <https://discourse.gohugo.io/t/solved-loop-through-data-file-with-keys/5193>\
**Category:** support\
**Created:** [January 19, 2017, 9:45pm UTC](https://discourse.gohugo.io/t/solved-loop-through-data-file-with-keys/5193 "2017-01-19T21:45:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kenold](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kenold/32/2463_2.png) [@kenold](https://discourse.gohugo.io/u/kenold)\
**Post date:** [January 19, 2017, 9:45pm UTC](https://discourse.gohugo.io/t/solved-loop-through-data-file-with-keys/5193/1 "2017-01-19T21:45:26Z")

</div>

How do I loop through this data file and display the quotes on each author’s page?  
Author pages are named: [jane-doe.md](http://jane-doe.md) and [john-smith.md](http://john-smith.md) in content/author  
quotes.yml

```
--
jane-doe:
  quotes:
    - quote: "quote 1 here"
      position: 1
    - quote: "quote 2 here"
      position: 2
john-smith:
  quotes:
    - quote: "quote 1 here"
      position: 1
    - quote: "quote 2 here"
      position: 2
    - quote: "quote 3 here"
      position: 3
---

```

in Jekyll, I could access each like this:  
`{{ site.data.quotes["jane-doe"].quotes }}`

---

<div class="post-metadata">

**Author:** ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)\
**Post date:** [January 20, 2017, 9:57pm UTC](https://discourse.gohugo.io/t/solved-loop-through-data-file-with-keys/5193/2 "2017-01-20T21:57:06Z")

</div>

Untested…

Assuming `data/quotes.yml`.

```auto
{{ range (index .Site.Data.quotes .File.TranslationBaseName).quotes }}
  {{ .position }}: {{ .quote }}
{{ end }}

```

Further Reading:

> **[Data Templates](http://gohugo.io/templates/data-templates/)**
>
> In addition to Hugo's built-in variables, you can specify your own custom data in templates or shortcodes that pull from both local and dynamic sources.

  

> **[Variables and Params](http://gohugo.io/variables/)**
>
> Page-, file-, taxonomy-, and site-level variables and parameters available in templates.

---

<div class="post-metadata">

**Author:** ![kenold](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kenold/32/2463_2.png) [@kenold](https://discourse.gohugo.io/u/kenold)\
**Post date:** [January 21, 2017, 6:38pm UTC](https://discourse.gohugo.io/t/solved-loop-through-data-file-with-keys/5193/3 "2017-01-21T18:38:34Z")

</div>

Thanks for the help. For some reasons it’s giving me a GO error, but if I hard code .File.TranslationBaseName with the actual file base name it worked:

```
{{ range (index .Site.Data.quotes "jane-doe").quotes }}
  {{ .position }}: {{ .quote }}
{{ end }}

```

So I’m guessing the error is from this line:  
`index .Site.Data.quotes .File.TranslationBaseName`  
I tried to put .File.TranslationBaseName in quotes, but still didn’t work.

---

<div class="post-metadata">

**Author:** ![kenold](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/kenold/32/2463_2.png) [@kenold](https://discourse.gohugo.io/u/kenold)\
**Post date:** [January 21, 2017, 7:59pm UTC](https://discourse.gohugo.io/t/solved-loop-through-data-file-with-keys/5193/4 "2017-01-21T19:59:55Z")

</div>

This works:

```
{{ $author := index .Site.Data.quotes (.File.TranslationBaseName) }}
{{ range $author.quotes }}
    {{ .quote }}
{{ end }}

```

Thanks @moorereason
