range
- Syntax
- range COLLECTION
Usage
The range statement iterates over a non-empty collection, binds the context (the dot) to successive elements, and executes the block. The collection may be a slice, a map, or an integer.
- When ranging over a slice, Hugo iterates over the elements in order.
- When ranging over a map, Hugo sorts the elements by key.
- When ranging over a positive integer
n, Hugo executes the blockntimes. The context starts at zero and increases by one in each iteration. Hugo does not execute the block if the integer is zero or negative.
Use the else statement to execute an alternate block if the collection is empty. Within the block, use the continue statement to stop the current iteration and continue with the next iteration, or the break statement to stop the current iteration and bypass any remaining iterations.
Within the block, the context is the current element, not the context passed into the template. For example, at the top of a page template, the context is a Page object, but within a range block that iterates over a slice of strings, the context is a string. Use the $ to access the template context from within the block.
To write effective templates, you must understand how context works.
See Go’s text/template documentation for more information.
Examples
These examples demonstrate ranging over different data types.
Slice of scalars
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
<p>{{ . }}</p>
{{ end }}The rendered output:
<p>foo</p>
<p>bar</p>
<p>baz</p>Assigning each element to a variable:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $v := $s }}
<p>{{ $v }}</p>
{{ end }}The rendered output:
<p>foo</p>
<p>bar</p>
<p>baz</p>Assigning the index and each element to variables:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $k, $v := $s }}
<p>{{ $k }}: {{ $v }}</p>
{{ end }}The rendered output:
<p>0: foo</p>
<p>1: bar</p>
<p>2: baz</p>With an else statement:
{{ $s := slice }}
{{ range $s }}
<p>{{ . }}</p>
{{ else }}
<p>The collection is empty</p>
{{ end }}The rendered output:
<p>The collection is empty</p>Accessing the template context from within the block:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
<p>{{ . }} is on the {{ $.Title }} page</p>
{{ end }}Slice of maps
{{ $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 }}The rendered output:
<p>John is 30</p>
<p>Will is 28</p>
<p>Joey is 24</p>Slice of pages
{{ range where site.RegularPages "Type" "articles" }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}The rendered output:
<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
{{ $m := dict "name" "John" "age" 30 }}
{{ range $k, $v := $m }}
<p>key = {{ $k }} value = {{ $v }}</p>
{{ end }}The rendered output, sorted by key:
<p>key = age value = 30</p>
<p>key = name value = John</p>Integers
{{ $s := slice }}
{{ range 3 }}
{{ $s = $s | append . }}
{{ end }}
{{ $s }} → [0 1 2]With a single iteration:
{{ $s := slice }}
{{ range 1 }}
{{ $s = $s | append . }}
{{ end }}
{{ $s }} → [0]Looping until a break condition is met, using the math.MaxInt64 function to set the maximum number of iterations:
{{ $n := 1 }}
{{ range math.MaxInt64 }}
{{ if gt $n 1000 }}
{{ break }}
{{ end }}
{{ $n = mul $n 2 }}
{{ end }}
{{ $n }} → 1024