hugo/content/en/functions/cond.md
Bjørn Erik Pedersen b9bd35d72e Squashed 'docs/' content from commit fdea5430f
git-subtree-dir: docs
git-subtree-split: fdea5430f89dfd849d39212abdf5ace0a4763e5a
2019-10-21 10:22:28 +02:00

1.1 KiB

title date description categories menu signature hugoversion relatedfuncs toc draft needsexamples
cond 2017-09-08 Return one of two arguments, depending on the value of a third argument.
functions
docs
parent
functions
cond CONTROL VAR1 VAR2
0.27
default
false false false

cond returns VAR1 if CONTROL is true, or VAR2 if it is not.

Example:

{{ cond (eq (len $geese) 1) "goose" "geese" }}

Would emit "goose" if the $geese array has exactly 1 item, or "geese" otherwise.

{{% warning %}} Whenever you use a cond function, both variable expressions are always evaluated. This means that a usage like cond false (div 1 0) 27 will throw an error because div 1 0 will be evaluated even though the condition is false.

In other words, the cond function does not provide short-circuit evaluation and does not work like a normal ternary operator that will pass over the first expression if the condition returns false. {{% /warning %}}