# Dynamically load datafiles based on variables

**URL:** <https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349>\
**Category:** support\
**Created:** [November 23, 2017, 8:56pm UTC](https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349 "2017-11-23T20:56:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![May\_Meow](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/may_meow/32/4641_2.png) [@May\_Meow](https://discourse.gohugo.io/u/May_Meow)\
**Post date:** [November 23, 2017, 8:56pm UTC](https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349/1 "2017-11-23T20:56:30Z")

</div>

How can i please load datafiles dynamically per page based on variables in header?

```auto
---
title: test
datafile: my-datafile.yml
---

```

How can i put this one into template? Thank you

---

<div class="post-metadata">

**Author:** ![digitalcraftsman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/digitalcraftsman/32/4142_2.png) [@digitalcraftsman](https://discourse.gohugo.io/u/digitalcraftsman)\
**Post date:** [November 23, 2017, 9:18pm UTC](https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349/2 "2017-11-23T21:18:35Z")

</div>

Hello @May_Meow,

try `.Site.data.(.Site.Params.datafile)`. Note, that the file extension / format isn’t used if you access the content of data files with `.Site.Data`. Hence it can be omitted in the front matter.

---

<div class="post-metadata">

**Author:** ![May\_Meow](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/may_meow/32/4641_2.png) [@May\_Meow](https://discourse.gohugo.io/u/May_Meow)\
**Post date:** [November 23, 2017, 10:32pm UTC](https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349/3 "2017-11-23T22:32:42Z")

</div>

`unexpected <.> in operand` it returns error

---

<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:** [November 23, 2017, 10:34pm UTC](https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349/4 "2017-11-23T22:34:34Z")

</div>

> [@digitalcraftsman](#):
>
> try .Site.data.(.Site.Params.datafile).

Should be something like:

```auto
{{ index .Site.Data .Params.datafile }}

```

---

<div class="post-metadata">

**Author:** ![maiki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/maiki/32/16384_2.png) [@maiki](https://discourse.gohugo.io/u/maiki)\
**Post date:** [November 23, 2017, 10:36pm UTC](https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349/5 "2017-11-23T22:36:12Z")

</div>

> [@May\_Meow](#):
>
> unexpected \<.\> in operand it returns error

This is not an appropriate response. Please see about getting help on our fora:

> [@Requesting Help](https://discourse.gohugo.io/t/requesting-help/9132):
>
> Please read and follow the below guidelines before asking for help in the forum. [DOCS](https://gohugo.io/documentation/) | [THEMES](https://themes.gohugo.io/) | [NEWS](https://gohugo.io/news/) | [GITHUB](https://github.com/gohugoio/hugo) | [FAQ](https://gohugo.io/troubleshooting/faq/) | [RELEASE NOTES](https://github.com/gohugoio/hugo/releases)How to Request Help Welcome to the Hugo forums and thanks for using Hugo! The below are a few friendly tips on getting help from the other Hugo volunteers in this forum. Some Knowledge Required Dare we say that while Hugo is indeed pretty magical, it is not meant to be a “magic wand” to suddenly give you a published website a la Square Space or Wordpress. You …

Basically, be as informative as possible, and share your code if you can. 🙂

---

<div class="post-metadata">

**Author:** ![May\_Meow](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/may_meow/32/4641_2.png) [@May\_Meow](https://discourse.gohugo.io/u/May_Meow)\
**Post date:** [November 23, 2017, 10:40pm UTC](https://discourse.gohugo.io/t/dynamically-load-datafiles-based-on-variables/9349/6 "2017-11-23T22:40:47Z")

</div>

So … i made it working 😃

my MD content file looks like this

```auto
---
title: "KRÚŽKY V ŠKOLSKOM ROKU 2017/2018"
date: 2017-11-22T15:49:53+01:00
draft: true
dataFile: kruzky
type: "data/kruzky"
---

Popis

```

and in `data/template` kruzky i calling following methods

```auto
{{ $data := index .Site.Data .Page.Params.dataFile }}
<h1>{{ $data.first.title }}</h1>
    <ul>
        {{range $data.first.list }}
        <li>{{ .name }} - {{ .teachers }}</li>
        {{ end }}
    </ul>

```

Thank you very much
