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