hugo/docs/content/en/functions/compare/Default.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

1.3 KiB

title description keywords action aliases
compare.Default Returns the second argument if set, else the first argument.
aliases related returnType signatures
default
functions/compare/Conditional
functions/go-template/Or
any
compare.Default DEFAULT INPUT
/functions/default

The default function returns the second argument if set, else the first argument.

{{% note %}} When the second argument is the boolean false value, the default function returns false. All other falsy values are considered unset.

{{% include "functions/go-template/_common/truthy-falsy.md" %}}

To set a default value based on truthiness, use the or operator instead.

{{% /note %}}

The default function returns the second argument if set:

{{ default 42 1 }} → 1
{{ default 42 "foo" }} → foo
{{ default 42 (dict "k" "v") }} → map[k:v]
{{ default 42 (slice "a" "b") }} → [a b]
{{ default 42 true }} → true

<!-- As noted above, the boolean "false" is considered set -->
{{ default 42 false }} → false

The default function returns the first argument if the second argument is not set:

{{ default 42 0 }} → 42
{{ default 42 "" }} → 42
{{ default 42 dict }} → 42
{{ default 42 slice }} → 42
{{ default 42 <nil> }} → 42