Working with data files

I have a data file that I can use to optionally specify an image and header color that should be used on a template page (the tag taxonomy page, specifically). Here’s the code that resembles what I want to do:

{{ $config := index .Site.Data.tag_headers (.Title | urlize) }}
{{ $image := index $config "image" }}
{{ $color := index $config "color" }}

The only problem is that it doesn’t work. The second and third lines fail. What’s the best way to go about what I’m trying to do? Specifically, I want to retrieve a map from a data file and then inspect its children. Note that the map may not exist.


Hehe. This looks correct. A tips would be to print the content of your data – with YAML etc. it is sometimes not always obvious if you get a map, slice an int, an int64 etc.

Have a look at printf in the Golang template documentation.