hugo/docs/content/en/functions/compare/Conditional.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.4 KiB

title description categories keywords action aliases
compare.Conditional Returns one of two arguments depending on the value of the control argument.
aliases related returnType signatures
cond
functions/compare/Default
any
compare.Conditional CONTROL ARG1 ARG2
/functions/cond

The CONTROL argument is a boolean value that indicates whether the function should return ARG1 or ARG2. If CONTROL is true, the function returns ARG1. Otherwise, the function returns ARG2.

{{ $qty := 42 }}
{{ cond (le $qty 3) "few" "many" }} → many

The CONTROL argument must be either true or false. To cast a non-boolean value to boolean, pass it through the not operator twice.

{{ cond (42 | not | not) "truthy" "falsy" }} → truthy
{{ cond ("" | not | not) "truthy" "falsy" }} → falsy

{{% note %}} Unlike ternary operators in other languages, the cond function does not perform short-circuit evaluation. The function evaluates both ARG1 and ARG2, regardless of the CONTROL value.

{{% /note %}}

Due to the absence of short-circuit evaluation, these examples throw an error:

{{ cond true "true" (div 1 0) }}
{{ cond false (div 1 0) "false" }}