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 The collection may be a slice, a map, or an integer.
15
16 ```go-html-template
17 {{ $s := slice "foo" "bar" "baz" }}
18 {{ range $s }}
19 {{ . }} → foo bar baz
20 {{ end }}
21 ```
22
23 Use with the [`else`][] statement:
24
25 ```go-html-template
26 {{ $s := slice "foo" "bar" "baz" }}
27 {{ range $s }}
28 <p>{{ . }}</p>
29 {{ else }}
30 <p>The collection is empty</p>
31 {{ end }}
32 ```
33
34 Within a range block:
35
36 - Use the [`continue`][] statement to stop the innermost iteration and continue to the next iteration
37 - Use the [`break`][] statement to stop the innermost iteration and bypass all remaining iterations
38
39 ## Understanding context
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 ### Slice of scalars
80
81 This template code:
82
83 ```go-html-template
84 {{ $s := slice "foo" "bar" "baz" }}
85 {{ range $s }}
86 <p>{{ . }}</p>
87 {{ end }}
88 ```
89
90 Is rendered to:
91
92 ```html
93 <p>foo</p>
94 <p>bar</p>
95 <p>baz</p>
96 ```
97
98 This template code:
99
100 ```go-html-template
101 {{ $s := slice "foo" "bar" "baz" }}
102 {{ range $v := $s }}
103 <p>{{ $v }}</p>
104 {{ end }}
105 ```
106
107 Is rendered to:
108
109 ```html
110 <p>foo</p>
111 <p>bar</p>
112 <p>baz</p>
113 ```
114
115 This template code:
116
117 ```go-html-template
118 {{ $s := slice "foo" "bar" "baz" }}
119 {{ range $k, $v := $s }}
120 <p>{{ $k }}: {{ $v }}</p>
121 {{ end }}
122 ```
123
124 Is rendered to:
125
126 ```html
127 <p>0: foo</p>
128 <p>1: bar</p>
129 <p>2: baz</p>
130 ```
131
132 ### Slice of maps
133
134 This template code:
135
136 ```go-html-template
137 {{ $m := slice
138 (dict "name" "John" "age" 30)
139 (dict "name" "Will" "age" 28)
140 (dict "name" "Joey" "age" 24)
141 }}
142 {{ range $m }}
143 <p>{{ .name }} is {{ .age }}</p>
144 {{ end }}
145 ```
146
147 Is rendered to:
148
149 ```html
150 <p>John is 30</p>
151 <p>Will is 28</p>
152 <p>Joey is 24</p>
153 ```
154
155 ### Slice of pages
156
157 This template code:
158
159 ```go-html-template
160 {{ range where site.RegularPages "Type" "articles" }}
161 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
162 {{ end }}
163 ```
164
165 Is rendered to:
166
167 ```html
168 <h2><a href="/articles/article-3/">Article 3</a></h2>
169 <h2><a href="/articles/article-2/">Article 2</a></h2>
170 <h2><a href="/articles/article-1/">Article 1</a></h2>
171 ```
172
173 ### Maps
174
175 This template code:
176
177 ```go-html-template
178 {{ $m := dict "name" "John" "age" 30 }}
179 {{ range $k, $v := $m }}
180 <p>key = {{ $k }} value = {{ $v }}</p>
181 {{ end }}
182 ```
183
184 Is rendered to:
185
186 ```go-html-template
187 <p>key = age value = 30</p>
188 <p>key = name value = John</p>
189 ```
190
191 Unlike ranging over an array or slice, Hugo sorts by key when ranging over a map.
192
193 ### Integers
194
195 Ranging over a positive integer `n` executes the block `n` times, with the context starting at zero and incrementing by one in each iteration.
196
197 ```go-html-template
198 {{ $s := slice }}
199 {{ range 1 }}
200 {{ $s = $s | append . }}
201 {{ end }}
202 {{ $s }} → [0]
203 ```
204
205 ```go-html-template
206 {{ $s := slice }}
207 {{ range 3 }}
208 {{ $s = $s | append . }}
209 {{ end }}
210 {{ $s }} → [0 1 2]
211 ```
212
213 Ranging over a non-positive integer executes the block zero times.
214
215 {{% include "/_common/functions/go-template/text-template.md" %}}
216
217 [`break`]: /functions/go-template/break/
218 [`continue`]: /functions/go-template/continue/
219 [`else`]: /functions/go-template/else/
220 [context]: /templates/introduction/#context