hugo/docs/content/en/functions/compare/Conditional.md
2024-11-13 11:07:57 +01:00

1,014 B

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

If CONTROL is truthy the function returns ARG1, otherwise it returns ARG2.

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

Unlike ternary operators in other languages, the compare.Conditional function does not perform short-circuit evaluation. It evaluates both ARG1 and ARG2 regardless of the CONTROL value.

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" }}