I am building a simple Hugo blog and I have this following toml config for a page
+++
[publications]
links = ["2017/article1",
"2017/article2"]
+++
And I have these files in their appropriate content section (content/publications/2017/article1.md). What I need is to iterate through them, load each page and use some of their .Params
in building a partial. Something like
{{ range .Params.publications.links }}
{{ do something with page parameters }}
{{ end }}
I guess it is a basic Hugo question, I just cant figure it out.
This actually requires some pretty advanced use of Hugo templates. But you can do it!
First, to make it easy for yourself, add the ".md" extension to the pages you are trying to access. It's probably also a good idea to add the full path so that Hugo doesn't get the wrong file if you add files with the same name in a different directory in the future.
+++
[publications]
links = ["publications/2017/article1.md",
"publications/2017/article2.md"]
+++
Then you can use something like the following in your template.
{{ range .Params.publications.links }}
{{ range where $.Site.Pages "URL" ($.RelRef .) }}
The "{{ .Title }}" page has {{ .WordCount }} words.
{{ end }}
{{ end }}
This uses the where
function to filter the array of all the site's pages by the URL field. To find the URL it uses the .RelRef
page variable with the link text.
I think there should also be a way to use the apply
function to do this without the inner range
statement, but I couldn't get it to work.