Hugo Custom Page And Site Variable

Sep 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>

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.