mirror of
https://github.com/gohugoio/hugoDocs.git
synced 2025-09-11 12:04:56 -04:00
Clarify .Page.Param method (#1953)
This commit is contained in:
parent
3fa1792d24
commit
27ca65463c
@ -1,40 +1,47 @@
|
|||||||
---
|
---
|
||||||
title: .Param
|
title: .Param
|
||||||
description: Calls page or site variables into your template.
|
description: Returns a page parameter, falling back to a site parameter if present.
|
||||||
date: 2017-02-01
|
signature: ['.Param KEY']
|
||||||
publishdate: 2017-02-01
|
|
||||||
lastmod: 2017-04-30
|
|
||||||
keywords: ["front matter"]
|
|
||||||
categories: [functions]
|
categories: [functions]
|
||||||
|
keywords: ['front matter', 'params']
|
||||||
menu:
|
menu:
|
||||||
docs:
|
docs:
|
||||||
parent: "functions"
|
parent: 'functions'
|
||||||
toc:
|
|
||||||
signature: [".Param KEY"]
|
|
||||||
workson: []
|
|
||||||
hugoversion:
|
|
||||||
relatedfuncs: [default]
|
|
||||||
deprecated: false
|
|
||||||
draft: false
|
|
||||||
aliases: []
|
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:
|
||||||
|
|
||||||
```
|
**Configuration**
|
||||||
$.Param "image"
|
|
||||||
```
|
|
||||||
|
|
||||||
{{% note %}}
|
{{< code-toggle file="config" copy=false >}}
|
||||||
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).
|
[params]
|
||||||
{{% /note %}}
|
display_toc = true
|
||||||
|
{{< /code-toggle >}}
|
||||||
|
|
||||||
|
**Content**
|
||||||
|
|
||||||
[configuration]: /getting-started/configuration/
|
{{< code-toggle file="content/about.md" fm=true copy=false >}}
|
||||||
[front matter]: /content-management/front-matter/
|
title = 'About'
|
||||||
[pagevars]: /variables/page/
|
date = 2023-01-01
|
||||||
[sitevars]: /variables/site/
|
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 >}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user