| 1 |
--- |
| 2 |
title: else |
| 3 |
description: Begins an alternate block for if, with, and range statements. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: |
| 10 |
signatures: [else VALUE] |
| 11 |
--- |
| 12 |
|
| 13 |
Use with the [`if`][] statement: |
| 14 |
|
| 15 |
```go-html-template |
| 16 |
{{ $var := "foo" }} |
| 17 |
{{ if $var }} |
| 18 |
{{ $var }} → foo |
| 19 |
{{ else }} |
| 20 |
{{ print "var is falsy" }} |
| 21 |
{{ end }} |
| 22 |
``` |
| 23 |
|
| 24 |
Use with the [`with`][] statement: |
| 25 |
|
| 26 |
```go-html-template |
| 27 |
{{ $var := "foo" }} |
| 28 |
{{ with $var }} |
| 29 |
{{ . }} → foo |
| 30 |
{{ else }} |
| 31 |
{{ print "var is falsy" }} |
| 32 |
{{ end }} |
| 33 |
``` |
| 34 |
|
| 35 |
Use with the [`range`][] statement: |
| 36 |
|
| 37 |
```go-html-template |
| 38 |
{{ $var := slice 1 2 3 }} |
| 39 |
{{ range $var }} |
| 40 |
{{ . }} → 1 2 3 |
| 41 |
{{ else }} |
| 42 |
{{ print "var is falsy" }} |
| 43 |
{{ end }} |
| 44 |
``` |
| 45 |
|
| 46 |
Use `else if` to check multiple conditions. |
| 47 |
|
| 48 |
```go-html-template |
| 49 |
{{ $var := 12 }} |
| 50 |
{{ if eq $var 6 }} |
| 51 |
{{ print "var is 6" }} |
| 52 |
{{ else if eq $var 7 }} |
| 53 |
{{ print "var is 7" }} |
| 54 |
{{ else if eq $var 42 }} |
| 55 |
{{ print "var is 42" }} |
| 56 |
{{ else }} |
| 57 |
{{ print "var is something else" }} |
| 58 |
{{ end }} |
| 59 |
``` |
| 60 |
|
| 61 |
{{% include "/_common/functions/go-template/text-template.md" %}} |
| 62 |
|
| 63 |
[`if`]: /functions/go-template/if/ |
| 64 |
[`range`]: /functions/go-template/range/ |
| 65 |
[`with`]: /functions/go-template/with/ |