Debian Codegraph @

docs/content/en/functions/go-template/if.md

raw
1 ---
2 title: if
3 description: Executes the block if the expression is truthy.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType:
10 signatures: [if EXPR]
11 ---
12
13 {{% include "/_common/functions/truthy-falsy.md" %}}
14
15 ```go-html-template
16 {{ $var := "foo" }}
17 {{ if $var }}
18 {{ $var }} → foo
19 {{ end }}
20 ```
21
22 Use with the [`else`][] statement:
23
24 ```go-html-template
25 {{ $var := "foo" }}
26 {{ if $var }}
27 {{ $var }} → foo
28 {{ else }}
29 {{ print "var is falsy" }}
30 {{ end }}
31 ```
32
33 Use `else if` to check multiple conditions:
34
35 ```go-html-template
36 {{ $var := 12 }}
37 {{ if eq $var 6 }}
38 {{ print "var is 6" }}
39 {{ else if eq $var 7 }}
40 {{ print "var is 7" }}
41 {{ else if eq $var 42 }}
42 {{ print "var is 42" }}
43 {{ else }}
44 {{ print "var is something else" }}
45 {{ end }}
46 ```
47
48 {{% include "/_common/functions/go-template/text-template.md" %}}
49
50 [`else`]: /functions/go-template/else/