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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
---
title: range
description: Iterates over a non-empty collection, binds context (the dot) to successive elements, and executes the block.
categories: []
keywords: []
action:
aliases: []
related:
- functions/go-template/break
- functions/go-template/continue
- functions/go-template/else
- functions/go-template/end
returnType:
signatures: [range COLLECTION]
aliases: [/functions/range]
toc: true
---
{{% include "functions/go-template/_common/truthy-falsy.md" %}}
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
{{ . }} → foo bar baz
{{ end }}
```
Use with the [`else`] statement:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
<p>{{ . }}</p>
{{ else }}
<p>The collection is empty</p>
{{ end }}
```
Within a range block:
- Use the [`continue`] statement to stop the innermost iteration and continue to the next iteration
- Use the [`break`] statement to stop the innermost iteration and bypass all remaining iterations
## Understanding context
At the top of a page template, the [context] (the dot) is a `Page` object. Within the `range` block, the context is bound to each successive element.
With this contrived example that uses the [`seq`] function to generate a slice of integers:
```go-html-template
{{ range seq 3 }}
{{ .Title }}
{{ end }}
```
Hugo will throw an error:
can't evaluate field Title in type int
The error occurs because we are trying to use the `.Title` method on an integer instead of a `Page` object. Within the `range` block, if we want to render the page title, we need to get the context passed into the template.
{{% note %}}
Use the `$` to get the context passed into the template.
{{% /note %}}
This template will render the page title three times:
```go-html-template
{{ range seq 3 }}
{{ $.Title }}
{{ end }}
```
{{% note %}}
Gaining a thorough understanding of context is critical for anyone writing template code.
{{% /note %}}
[`seq`]: /functions/collections/seq/
[context]: /getting-started/glossary/#context
## Array or slice of scalars
This template code:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
<p>{{ . }}</p>
{{ end }}
```
Is rendered to:
```html
<p>foo</p>
<p>bar</p>
<p>baz</p>
```
This template code:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $v := $s }}
<p>{{ $v }}</p>
{{ end }}
```
Is rendered to:
```html
<p>foo</p>
<p>bar</p>
<p>baz</p>
```
This template code:
```go-html-template
{{ $s := slice "foo" "bar" "baz" }}
{{ range $k, $v := $s }}
<p>{{ $k }}: {{ $v }}</p>
{{ end }}
```
Is rendered to:
```html
<p>0: foo</p>
<p>1: bar</p>
<p>2: baz</p>
```
## Array or slice of maps
This template code:
```go-html-template
{{ $m := slice
(dict "name" "John" "age" 30)
(dict "name" "Will" "age" 28)
(dict "name" "Joey" "age" 24)
}}
{{ range $m }}
<p>{{ .name }} is {{ .age }}</p>
{{ end }}
```
Is rendered to:
```html
<p>John is 30</p>
<p>Will is 28</p>
<p>Joey is 24</p>
```
## Array or slice of pages
This template code:
```go-html-template
{{ range where site.RegularPages "Type" "articles" }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
```
Is rendered to:
```html
<h2><a href="/articles/article-3/">Article 3</a></h2>
<h2><a href="/articles/article-2/">Article 2</a></h2>
<h2><a href="/articles/article-1/">Article 1</a></h2>
```
## Maps
This template code:
```go-html-template
{{ $m := dict "name" "John" "age" 30 }}
{{ range $k, $v := $m }}
<p>key = {{ $k }} value = {{ $v }}</p>
{{ end }}
```
Is rendered to:
```go-html-template
<p>key = age value = 30</p>
<p>key = name value = John</p>
```
Unlike ranging over an array or slice, Hugo sorts by key when ranging over a map.
{{% include "functions/go-template/_common/text-template.md" %}}
[`else`]: /functions/go-template/else/
[`break`]: /functions/go-template/break/
[`continue`]: /functions/go-template/continue/
|