.Param
Syntax
.Param KEY
The .Param
method on .Page
looks for the given KEY
in page parameters, and returns the corresponding value. If it cannot find the KEY
in page parameters, it looks for the KEY
in site parameters. If it cannot find the KEY
in either location, the .Param
method returns nil
.
Site and theme developers commonly set parameters at the site level, allowing content authors to override those parameters at the page level.
For example, to show a table of contents on every page, but allow authors to hide the table of contents as needed:
Configuration
config.
params:
display_toc: true
[params]
display_toc = true
{
"params": {
"display_toc": true
}
}
Content
content/about.md
---
date: "2023-01-01"
display_toc: false
draft: false
title: About
---
+++
date = 2023-01-01
display_toc = false
draft = false
title = 'About'
+++
{
"date": "2023-01-01",
"display_toc": false,
"draft": false,
"title": "About"
}
Template
layouts/_default/single.html
{{ if .Param "display_toc" }}
{{ .TableOfContents }}
{{ end }}
The .Param
method returns the value associated with the given KEY
, regardless of whether the value is truthy or falsy. If you need to ignore falsy values, use this construct instead:
layouts/_default/single.html
{{ or .Params.foo site.Params.foo }}