HUGO
Menu
GitHub 89864 stars Mastodon

compare.Conditional

Returns one of two arguments depending on the value of the control argument.
Syntax
compare.Conditional CONTROL ARG1 ARG2
Returns
any
Alias
cond

Usage

The compare.Conditional function returns one of two arguments depending on the value of the control argument. If CONTROL is truthy the function returns ARG1, otherwise it returns ARG2.

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.

Examples

{{ $qty := 42 }}
{{ compare.Conditional (compare.Le $qty 3) "few" "many" }} → many

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

{{ compare.Conditional true "true" (div 1 0) }}
{{ compare.Conditional false (div 1 0) "false" }}