hugo/docs/content/en/functions/go-template/with.md
2023-10-20 09:43:56 +02:00

1.4 KiB

title description categories keywords menu function relatedFunctions aliases
with Rebinds the context (`.`) within its scope and skips the block if the variable is absent or empty.
functions
docs
parent
functions
aliases returnType signatures
any
with PIPELINE
with
range
/functions/with

{{% readfile file="/functions/_common/go-template-functions.md" %}}

An alternative way of writing an if statement and then referencing the same value is to use with instead. with rebinds the context (.) within its scope and skips the block if the variable is absent, unset or empty.

The set of empty values is defined by the Go templates package. Empty values include false, the number zero, and the empty string.

If you want to render a block if an index or key is present in a slice, array, channel or map, regardless of whether the value is empty, you should use isset instead.

The following example checks for a user-defined site variable called twitteruser. If the key-value is not set, the following will render nothing:

{{< code file="layouts/partials/twitter.html" >}} {{ with .Site.Params.twitteruser }} {{ end }} {{< /code >}}