Hugo

Dynamically load datafiles based on variables

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

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

How can i put this one into template? Thank you

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.

unexpected <.> in operand it returns error

Should be something like:

{{ index .Site.Data .Params.datafile }}
1 Like

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

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

So … i made it working :smiley:

my MD content file looks like this

---
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

{{ $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

2 Likes