I try to add custom data to pages in Zola. My toml header looks something like this:
+++
title = "Some book"
authors = "Mr. A, Mrs. B"
url = "https://www.books.com/blabla"
cover = "isbn.jpg"
+++
I want to access the additional fields in my templates. According to the docs I would expect the data to show up in the extra
field. It tried it like this:
{% for p in section.pages %}
<li>{{ p.title }}
<img src='{{ p.extra["cover"] }}'></img>
</li>
{% endfor %}
The title is displayed correctly, so iterating the pages in general works fine. But extra
is empty. I also tried to iterate over extra
and to display the content, but it seems to always be empty.
How do I access "extra data" in my templates?
You're not setting any extra data in your header, so of course the p.extra
map is empty.
Try setting the data:
+++
title = "foo"
[extra]
cover = "isbn.jpg"
+++
That piece between the +++
is just plain TOML, so you have to define a new section "extra", which is what you do with the [extra]
notation shown above!