Clarify .Page.Param method (#1953)

This commit is contained in:
Joe Mooring 2023-01-11 13:06:41 -08:00 committed by GitHub
parent 3fa1792d24
commit 27ca65463c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,40 +1,47 @@
---
title: .Param
description: Calls page or site variables into your template.
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-04-30
keywords: ["front matter"]
description: Returns a page parameter, falling back to a site parameter if present.
signature: ['.Param KEY']
categories: [functions]
keywords: ['front matter', 'params']
menu:
docs:
parent: "functions"
toc:
signature: [".Param KEY"]
workson: []
hugoversion:
relatedfuncs: [default]
deprecated: false
draft: false
parent: 'functions'
aliases: []
---
In Hugo, you can declare [site-wide params][sitevars] (i.e. in your [configuration]), as well as params for [individual pages][pagevars].
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`.
A common use case is to have a general value for the site and a more specific value for some of the pages (e.g., an image).
Site and theme developers commonly set parameters at the site level, allowing content authors to override those parameters at the page level.
You can use the `.Param` method to call these values into your template. The following will first look for an `image` param in a specific content's [front matter]. If not found, Hugo will look for an `image` param in your site's configuration:
For example, to show a table of contents on every page, but allow authors to hide the table of contents as needed:
```
$.Param "image"
```
**Configuration**
{{% note %}}
The `Param` method may not consider empty strings in a content's front matter as "not found." If you are setting preconfigured front matter fields to empty strings using Hugo's archetypes, it may be best to use the [`default` function](/functions/default/) instead of `Param`. See the [related issue on GitHub](https://github.com/gohugoio/hugo/issues/3366).
{{% /note %}}
{{< code-toggle file="config" copy=false >}}
[params]
display_toc = true
{{< /code-toggle >}}
**Content**
[configuration]: /getting-started/configuration/
[front matter]: /content-management/front-matter/
[pagevars]: /variables/page/
[sitevars]: /variables/site/
{{< code-toggle file="content/about.md" fm=true copy=false >}}
title = 'About'
date = 2023-01-01
draft = false
display_toc = false
{{< /code-toggle >}}
**Template**
{{< code file="layouts/_default/single.html" copy="false" >}}
{{ if .Param "display_toc" }}
{{ .TableOfContents }}
{{ end }}
{{< /code >}}
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:
{{< code file="layouts/_default/single.html" copy="false" >}}
{{ or .Params.foo site.Params.foo }}
{{< /code >}}