Hugo Custom Page And Site Variable

September 9, 2017

Page Variable

You create create custom variable for page as per the following configration for the page markdown file.

---
title: "Post"
date: 2017-09-09T22:14:20+08:00
test: "OK"
testAgain: "This will work"
nested:
  test: "OK"
  testAgain: "This will not work"
---

You can access the custom variable using .Params from your template files.

<pre>
[page]
test={{ .Params.test }}
testAgain={{ .Params.testAgain }}
nested.test={{ .Params.nested.test }}
</pre>

Note: For Hugo v0.25.1, accessing custom nested page variable with camel case doesn’t work, such as the case for .Params.nested.testAgain, but it will work for non-nested variable.

Query pages using custom page variable

You can use custom page variable when querying for .Site.Pages.

{{ $pages := (where .Site.Pages "Params.test" "OK") }}
{{ range $pages }}
  {{ .Title }} - {{ .Permalink }}
{{ end }}

Site Variable

Edit config.toml.

[params]
  test = "OK"

  [params.nested]
    test = "OK"

You can access the custom variable using .Site.Params from your template files.

<pre>
[site]
test={{ .Site.Params.test }}
nested.test={{ .Site.Params.nested.test }}
</pre>
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.