How to Display Values from Nested YAML Frontmatter

I’m having trouble working with nested YAML frontmatter. I’ve tried examples from here on the discourse, and from some examples I’ve seen on blogs, but I still can’t get it to work, even when I use exactly the same code from the examples. Could anyone talk me through how to do it?

Here’s an example. Let’s say I’ve got posts for book reviews. The frontmatter could look something like the following.

review-01.md

---
title: "Name of Book Review"
author: "John Doe"
book:
  - name: "Some Plays"
  - author: "William Shakespeare"
---

now, in my template I might have something like the following.

review.html

<p>
This review is called {{ .Params.title }}. 
It's about a book by {{ .Params.book.author }}.
</p>

I’d expect it to say “This review is called Name of Book Review. It’s about a book by William Shakespeare.” But I can’t get it to display the “William Shakespeare” part.

The first title works. But if I add the second value for the book’s author, I get an error like: template failed at <.Params.book.author>: can’t evaluate field name in type []interface {}.

What should I be doing instead, to display the nested values?

I’m running hugo version hugo v0.102.3

Your YAML is not formatted correctly.

Sometimes I find it helpful to visualize the data structure using a tool like this.

I think you want this:

title: "Name of Book Review"
author: "John Doe"
book:
  name: "Some Plays"
  author: "William Shakespeare"

I get the same error when the YAML is formatted that way.

Then you are doing something else wrong.

Please post a link to the public repository for your project.

See https://discourse.gohugo.io/t/requesting-help/9132.

Let us see your code

Include a link to the source code repository of your project, because we really need the context of seeing your templates and partials to be able to help you. It is trivial to do a quick git clone on your repo, then run hugo server in your project, to help you out. On the other hand, recreating your code from screenshots, or sort of guessing at it, is not.

If you can’t share your repository for whatever reason, consider creating a dummy repo that you can share, which reproduces the problem you’re experiencing.

1 Like

No semicolon?

I’ve checked my code and the missing semicolon is only a typo in my example above, which I’ll revise. But good catch!

I’m also working up a repo I can share to demonstrate this in a fuller context.

1 Like

ok i made an repo to demonstrate the issue, using YAML that passed through two different validators (slightly revised from original example above.) It’s up at GitHub - dylan-k/hugoBasicExample: Example site to use for testing

You have this:

title: "Name of Book Review"
author: "John Doe"
book: 
    - 
      name: "Some Plays"
      author: "William Shakespeare"

It should be this, as I described in my original response:

title: "Name of Book Review"
author: "John Doe"
book: 
  name: "Some Plays"
  author: "William Shakespeare"
1 Like

Indeed, that’s the fix. Thank you so very much!

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