Debian Codegraph @

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

raw
1 ---
2 title: range
3 description: Iterates over a non-empty collection, binds context (the dot) to successive elements, and executes the block.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType:
10 signatures: [range COLLECTION]
11 aliases: [/functions/range]
12 ---
13
14 ## Usage
15
16 The collection may be a slice, a map, or an integer.
17
18 ```go-html-template
19 {{ $s := slice "foo" "bar" "baz" }}
20 {{ range $s }}
21 {{ . }} → foo bar baz
22 {{ end }}
23 ```
24
25 Use with the [`else`][] statement:
26
27 ```go-html-template
28 {{ $s := slice "foo" "bar" "baz" }}
29 {{ range $s }}
30 <p>{{ . }}</p>
31 {{ else }}
32 <p>The collection is empty</p>
33 {{ end }}
34 ```
35
36 Within a range block:
37
38 - Use the [`continue`][] statement to stop the innermost iteration and continue to the next iteration
39 - Use the [`break`][] statement to stop the innermost iteration and bypass all remaining iterations
40
41 See the [context][] section in the introduction to templating.
42
43 For example, at the top of a _page_ template, the [context](g) (the dot) is a `Page` object. Within the `range` block, the context is bound to each successive element.
44
45 With this contrived example:
46
47 ```go-html-template
48 {{ $s := slice "foo" "bar" "baz" }}
49 {{ range $s }}
50 {{ .Title }}
51 {{ end }}
52 ```
53
54 Hugo will throw an error:
55
56 ```text
57 can't evaluate field Title in type int
58 ```
59
60 The error occurs because we are trying to use the `Title` method on a string 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.
61
62 > [!NOTE]
63 > Use the `$` to get the context passed into the template.
64
65 This template will render the page title three times:
66
67 ```go-html-template
68 {{ $s := slice "foo" "bar" "baz" }}
69 {{ range $s }}
70 {{ $.Title }}
71 {{ end }}
72 ```
73
74 > [!NOTE]
75 > Gaining a thorough understanding of context is critical for anyone writing template code.
76
77 ## Examples
78
79 The following examples demonstrate ranging over different collection types.
80
81 ### Slice of scalars
82
83 This template code:
84
85 ```go-html-template
86 {{ $s := slice "foo" "bar" "baz" }}
87 {{ range $s }}
88 <p>{{ . }}</p>
89 {{ end }}
90 ```
91
92 Is rendered to:
93
94 ```html
95 <p>foo</p>
96 <p>bar</p>
97 <p>baz</p>
98 ```
99
100 This template code:
101
102 ```go-html-template
103 {{ $s := slice "foo" "bar" "baz" }}
104 {{ range $v := $s }}
105 <p>{{ $v }}</p>
106 {{ end }}
107 ```
108
109 Is rendered to:
110
111 ```html
112 <p>foo</p>
113 <p>bar</p>
114 <p>baz</p>
115 ```
116
117 This template code:
118
119 ```go-html-template
120 {{ $s := slice "foo" "bar" "baz" }}
121 {{ range $k, $v := $s }}
122 <p>{{ $k }}: {{ $v }}</p>
123 {{ end }}
124 ```
125
126 Is rendered to:
127
128 ```html
129 <p>0: foo</p>
130 <p>1: bar</p>
131 <p>2: baz</p>
132 ```
133
134 ### Slice of maps
135
136 This template code:
137
138 ```go-html-template
139 {{ $m := slice
140 (dict "name" "John" "age" 30)
141 (dict "name" "Will" "age" 28)
142 (dict "name" "Joey" "age" 24)
143 }}
144 {{ range $m }}
145 <p>{{ .name }} is {{ .age }}</p>
146 {{ end }}
147 ```
148
149 Is rendered to:
150
151 ```html
152 <p>John is 30</p>
153 <p>Will is 28</p>
154 <p>Joey is 24</p>
155 ```
156
157 ### Slice of pages
158
159 This template code:
160
161 ```go-html-template
162 {{ range where site.RegularPages "Type" "articles" }}
163 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
164 {{ end }}
165 ```
166
167 Is rendered to:
168
169 ```html
170 <h2><a href="/articles/article-3/">Article 3</a></h2>
171 <h2><a href="/articles/article-2/">Article 2</a></h2>
172 <h2><a href="/articles/article-1/">Article 1</a></h2>
173 ```
174
175 ### Maps
176
177 This template code:
178
179 ```go-html-template
180 {{ $m := dict "name" "John" "age" 30 }}
181 {{ range $k, $v := $m }}
182 <p>key = {{ $k }} value = {{ $v }}</p>
183 {{ end }}
184 ```
185
186 Is rendered to:
187
188 ```go-html-template
189 <p>key = age value = 30</p>
190 <p>key = name value = John</p>
191 ```
192
193 Unlike ranging over an array or slice, Hugo sorts by key when ranging over a map.
194
195 ### Integers
196
197 Ranging over a positive integer `n` executes the block `n` times, with the context starting at zero and incrementing by one in each iteration.
198
199 ```go-html-template
200 {{ $s := slice }}
201 {{ range 1 }}
202 {{ $s = $s | append . }}
203 {{ end }}
204 {{ $s }} → [0]
205 ```
206
207 ```go-html-template
208 {{ $s := slice }}
209 {{ range 3 }}
210 {{ $s = $s | append . }}
211 {{ end }}
212 {{ $s }} → [0 1 2]
213 ```
214
215 Ranging over a non-positive integer executes the block zero times.
216
217 {{% include "/_common/functions/go-template/text-template.md" %}}
218
219 [`break`]: /functions/go-template/break/
220 [`continue`]: /functions/go-template/continue/
221 [`else`]: /functions/go-template/else/
222 [context]: /templates/introduction/#context