Get Data Template with custom variables

Hello, let’s say I have a lot of json data files with lyrics, which is categorized as such:

  • data > artists > {artist_name} > songs > {song_name}.json

In my Content markdown files then I have parameters for {artist_name} and an array of song names.

My question is, how can I call {{ .Site.Data.artists.$artist_name.songs.$song_name }}?

I’ve tried to use e.g.
{{ range .Params.songs }}
{{ filename := printf ".Site.Data.artists.%s.songs.%s" .Params.artist . }}
{{ end }}

Which gets the right path, but I’m unable to use the string to get the json data… it also seems it’s impossible to say, e.g. {{ .Site.Data “artists.adele.songs.someonelikeyou” }}.

The only way I can think of that might work is range over everything for both artist and song and check if the json field match the param, but that sounds extremely inefficient.

Any help would be much appreciated :slight_smile:

You can use the index function: index | Hugo

So something like

{{ $artist := .Params.artist_name }}

{{ range .Params.songs }}
  {{ $s := . }}
  {{ $artistData := index site.Data.artists $artist }}
  {{ index $artistData.songs $s }}

{{ end }}
1 Like

Wow great, thank you so much :slight_smile:

I tried to use index before, but guess I didn’t understand it properly… perhaps because I used it in connection with range. But now it makes a lot of sense :+1:

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