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

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.

1 Like

This is untested, but should work:

{{% getdata startdata %}}

Then in shortcode:

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

Thanks very much.

Now tested, and it works!

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

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

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

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

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

It’s builtin func in Go stdlib:

http://golang.org/pkg/text/template/

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.
1 Like

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

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