hugo/docs/content/en/functions/collections/IsSet.md
2023-10-20 09:43:56 +02:00

1.3 KiB

title linkTitle description categories keywords menu function relatedFunctions aliases
collections.IsSet isset Reports whether the key exists within the collection.
functions
docs
parent
functions
aliases returnType signatures
isset
bool
collections.IsSet COLLECTION KEY
collections.Dictionary
collections.Group
collections.Index
collections.IsSet
collections.Where
/functions/isset

For example, consider this site configuration:

{{< code-toggle file=hugo copy=false >}} [params] showHeroImage = false {{< /code-toggle >}}

It the value of showHeroImage is true, we can detect that it exists using either if or with:

{{ if site.Params.showHeroImage }}
  {{ site.Params.showHeroImage }} → true
{{ end }}

{{ with site.Params.showHeroImage }}
  {{ . }} → true
{{ end }}

But if the value of showHeroImage is false, we can't use either if or with to detect its existence. In this case, you must use the isset function:

{{ if isset site.Params "showheroimage" }}
  <p>The showHeroImage parameter is set to {{ site.Params.showHeroImage }}.<p>
{{ end }}

{{% note %}} When using the isset function you must reference the key using lower case. See the previous example. {{% /note %}}