Embedded Data in Front Matter

This may seem like an easy question, but I’ve been fighting it for a couple of days. I have a section labelled “music” that basically is a set of blog posts containing mp3 files from performances by our local community band. In the past, it was a simple HTML file, but I’d like to do it properly. I tried adding it to my front matter like:

[[params.music]]
url = "hugo.mp3"
name = “The Hugo Song”
[[params.music]]
url = "html.mp3"
name = “The HTML Song”

But I couldn’t parse that. I could put the TOML in a data file, but how can I specify that particular data file dynamically with the single.html template in the music layout folder? I’d have about 100 separate data files + 100 posts. That’s why I’d like to put it into the frontmatter if possible.

I’m not sure if this is related to https://github.com/spf13/hugo/pull/2841

I tried again using YAML instead of TOML since TOML is broken… My front matter excerpt looks like:

tags:         "Community Band"
music:        " Community Band"
playlist:
  - songs:
    - title: "Big Band Signatures"
      url: "bigbandsignatures.mp3"
   .- title: "Florentiner March"
     .url: "florentiner.mp3"
    - title: "New Colonial March"
      url: "newcolonial.mp3"

In my single.html template, my code should be something like this if I am correct:

      {{ range .Params.playlist.songs }}<li>{{.title}}</li>{{end}}

However, when I build, I get errored out on that… What am I doing wrong?

I’ve handled arrays like this in site before (to generate nested / multileveled menus for bootstrap), but this isn’t working for the page level array.

What’s the error message that you got? The issue that that pull request fixes is about a TOML array not parsed and turning up empty (without an error message). Perhaps the error message you got can help troubleshoot this issue.

ERROR: 2017/01/19 18:56:33 general.go:236: Error while rendering page music\2010-music-study-club-concert.md: template: theme/music/single.html:12:26: executing "theme/mu…

I originally had the data in toml just like the menu structure, but it just rendered blank lines or errored out… That’s when I swapped over to YAML because it was noted in the past that this was supposed to work…

The line number of that error is the {{ range… line. Not much about the error. When I print the page params, all the data shows up, so it is read successfully…

Here is the printf of the .Params:

map[string]interface {}{“playlist”:[]interface {}{map[interface {}]interface {}{“songs”:[]interface {}{map[interface {}]interface {}{“title”:“Big Band Signatures”, “url”:“bigbandsignatures.mp3”}, map[interface {}]interface {}{“title”:“Florentiner March”, “url”:“florentiner.mp3”}, map[interface {}]interface {}{“title”:“New Colonial March”, “url”:“newcolonial.mp3”}, map[interface {}]interface {}{“url”:“starsandstripes.mp3”, “title”:“Stars and Stripes Forever”}, map[interface {}]interface {}{“title”:“Symphonic Dances from Fiddler on the Roof”, “url”:“symphonicdances.mp3”}, map[interface {}]interface {}{“title”:“The Prayer”, “url”:“theprayer.mp3”}, map[interface {}]interface {}{“title”:“The Saints”, “url”:“thesaints.mp3”}, map[interface {}]interface {}{“url”:“tiptoe.mp3”, “title”:“Tiptoe through the Tubas”}}}}, “categories”:[]string{“music”}, “tags”:[]string{“Comal Community Band”}, “music”:“Comal Community Band”}

You should remove the extra dots at the beginning of these lines.

.- title: “Florentiner March”
.url: “florentiner.mp3”

Try a nested range:

{{range .Params.playlist}}
	{{range .Params.songs}}
		{{.title}}
	{{end}}
{{end}}

I actually didn’t have the dots in those lines… I had that when I was trying to fake indent on this site until I discovered that the four spaces indented code…

It works if I change it to:

{{range .Params.playlist}}
  {{range .songs}}
	{{.title}}
  {{end}}
{{end}}

Thank you… :slight_smile:

Glad it worked! :wink: