summaryrefslogtreecommitdiffstats
path: root/docs/content/en/functions/compare/Cond.md
blob: 4b92a893c10e2f4036c4287b78baf86063382ec0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
title: compare.Conditional
linkTitle: cond
description: Returns one of two arguments depending on the value of the control argument.
categories: [functions]
keywords: []
menu:
  docs:
    parent: functions
function:
  aliases: [cond]
  returnType: any
  signatures: [compare.Conditional CONTROL ARG1 ARG2]
relatedFunctions:
  - compare.Conditional
  - compare.Default
aliases: [/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.

```go-html-template
{{ $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.

```go-html-template
{{ 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.

[short-circuit evaluation]: https://en.wikipedia.org/wiki/Short-circuit_evaluation
[ternary operators]: https://en.wikipedia.org/wiki/Ternary_conditional_operator
{{% /note %}}


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

```go-html-template
{{ cond true "true" (div 1 0) }}
{{ cond false (div 1 0) "false" }}
```