Debian Codegraph @

docs/content/en/functions/compare/Conditional.md

raw
1 ---
2 title: compare.Conditional
3 description: Returns one of two arguments depending on the value of the control argument.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [cond]
9 returnType: any
10 signatures: [compare.Conditional CONTROL ARG1 ARG2]
11 aliases: [/functions/cond]
12 ---
13
14 ## Usage
15
16 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`.
17
18 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.
19
20 ## Examples
21
22 ```go-html-template
23 {{ $qty := 42 }}
24 {{ compare.Conditional (compare.Le $qty 3) "few" "many" }} → many
25 ```
26
27 Due to the absence of short-circuit evaluation, these examples throw an error:
28
29 ```go-html-template
30 {{ compare.Conditional true "true" (div 1 0) }}
31 {{ compare.Conditional false (div 1 0) "false" }}
32 ```
33
34 [short-circuit evaluation]: https://en.wikipedia.org/wiki/Short-circuit_evaluation
35 [ternary operators]: https://en.wikipedia.org/wiki/Ternary_conditional_operator