# Inserting data from data file into content file (newbie question)

**URL:** <https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002>\
**Category:** support\
**Created:** [April 1, 2015, 9:20am UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002 "2015-04-01T09:20:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomcl](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@tomcl](https://discourse.gohugo.io/u/tomcl)\
**Post date:** [April 1, 2015, 9:20am UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/1 "2015-04-01T09:20:56Z")

</div>

Hi,

I’m getting to grips with hugo, having never before used a static website generator. It seems very nice.  
I’m using hugo v0.13.

I have a use case (common?) where I want to have a collection of variables defined in a data file `/data/myfile.yaml` whose values I can insert by name as needed into various content files. (In my use case these represent calendar dates that get updated every year). For example:  
startdate: 13 Jan 2015  
enddata: 30 Jun 2015

I think what I need is to define a shortcode named `getdata` with a single positional parameter.  
Inside a content file this would be used set to the name of the data variable I want to insert:

```
## content file use case
Start date is {{%getdata startdata%}}, end date is {{%getdata enddata%}}

```

The shortcode defined as file `getdata.html` in `/layout/shortcodes` would then be:

```
  {{.Data.myfile.{{.Get 0}}}}

```

Here the inner `{{}}` are needed to access the value of the shortcode parameter, and the outer `{{}}` are needed to access the value of the desired variable `.Data.myfile.startdata` or `.Data.myfile.enddata`

Is this type of expansion possible? I’m unclear the exact semantics of variables inside shortcodes and reckon if I try to do this by trial and error it will take a long time.

Also: are the names here case sensitive? I’m not at the moment quite sure when a capitalised first letter is required: `.Data` and when not `.myfile`, or anything defined under `.Params` (seems to be a change in v0.13)?

Thanks very much for bearing with what is an uninformed question.

---

<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:** [April 1, 2015, 11:14am UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/2 "2015-04-01T11:14:17Z")

</div>

This is untested, but should work:

```
{{% getdata startdata %}}

```

Then in shortcode:

```
{{ $data := .Get 0 }}
{{ index .Page.Site.Data.myfile $data }}
```

---

<div class="post-metadata">

**Author:** ![tomcl](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@tomcl](https://discourse.gohugo.io/u/tomcl)\
**Post date:** [April 1, 2015, 11:54am UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/3 "2015-04-01T11:54:31Z")

</div>

Thanks very much.

Now tested, and it works!

---

<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:** [April 1, 2015, 1:15pm UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/4 "2015-04-01T13:15:00Z")

</div>

> [@bjornerik](#):
>
> {{ index .Page.Site.Data.myfile $data }}

Note that you cannot nest {{}} inside eachother, but the above can be rewritten using parens:

```
{{ index .Page.Site.Data.myfile (.Get 0) }}

```

---

<div class="post-metadata">

**Author:** ![Joholland](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/joholland/32/405_2.png) [@Joholland](https://discourse.gohugo.io/u/Joholland)\
**Post date:** [April 23, 2015, 9:53pm UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/5 "2015-04-23T21:53:32Z")

</div>

Trying to do something similar, I have a YAML file in /data/ that I generate a table from. We want to use a shortcode in markdown to create the table, and pass a specific yaml key to the shortcode so it knows what data file to use.

Using the example above, I get this:

ERROR: 2015/04/23 error processing shortcode shortcodes/providerTable.html  
ERR: template: shortcodes/providerTable.html:8:22: executing “shortcodes/providerTable.html” at \<.Page.Site.Data…\>: systemProviders is not a method but has arguments

---

<div class="post-metadata">

**Author:** ![Joholland](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/joholland/32/405_2.png) [@Joholland](https://discourse.gohugo.io/u/Joholland)\
**Post date:** [April 23, 2015, 10:12pm UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/6 "2015-04-23T22:12:49Z")

</div>

I was missing the call to the index function. I managed to get this working now.

---

<div class="post-metadata">

**Author:** ![rhewitt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rhewitt/32/332_2.png) [@rhewitt](https://discourse.gohugo.io/u/rhewitt)\
**Post date:** [April 28, 2015, 7:38pm UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/7 "2015-04-28T19:38:47Z")

</div>

@bjornerik or @Joholland could you explain the `index` function? I see it used in the go-template primer, but it isn’t explained.

---

<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:** [April 28, 2015, 8:02pm UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/8 "2015-04-28T20:02:01Z")

</div>

It’s builtin func in Go stdlib:

[http://golang.org/pkg/text/template/](http://golang.org/pkg/text/template/)

```auto
Returns the result of indexing its first argument by the
    following arguments. Thus "index x 1 2 3" is, in Go syntax,
    x[1][2][3]. Each indexed item must be a map, slice, or array.

```

---

<div class="post-metadata">

**Author:** ![RickCogley](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/rickcogley/32/10269_2.png) [@RickCogley](https://discourse.gohugo.io/u/RickCogley)\
**Post date:** [April 28, 2015, 10:06pm UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/9 "2015-04-28T22:06:24Z")

</div>

This is cool. You could use it for a glossary function, I suppose.

---

<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:** [April 28, 2015, 10:57pm UTC](https://discourse.gohugo.io/t/inserting-data-from-data-file-into-content-file-newbie-question/1002/10 "2015-04-28T22:57:38Z")

</div>

Yes, it is a powerful tool – the name is a bit misleading, though, as it works fine for maps as well.
