HUGO

Get data by dynamic string

I have couple of data files and I list in duplicated layout files.

{{ with .Site.Data.money_transfer.DZ.sections }}
  <div >
    {{ range .items }}
      {{ partial "section.html" . }}
    {{ end }}
  </div>
{{ end }}

Can I call like this?

{{ with .Site.Data.money_transfer["DZ"].sections }}
...
{{ end }}

Because if I can, I can call dynamically with only one layout

You may be looking for the index function. Here’s a sample:

{{ $key := "DZ" }}
{{ with (index .Site.Data.money_transfer $key).sections }}

The $key can be any string, or read from a front matter param.

2 Likes

that’s great! thank you for reply!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.