mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
2.2 KiB
2.2 KiB
title | description | categories | keywords | menu | function | relatedFunctions | aliases | toc | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
range | Iterates over slices, maps, and page collections. |
|
|
|
|
|
true |
{{% readfile file="/functions/_common/go-template-functions.md" %}}
Slices
Template:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
<p>{{ . }}</p>
{{ end }}
Result:
<p>foo</p>
<p>bar</p>
<p>baz</p>
Template:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $v := $s }}
<p>{{ $v }}</p>
{{ end }}
Result:
<p>foo</p>
<p>bar</p>
<p>baz</p>
Template:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $k, $v := $s }}
<p>{{ $k }}: {{ $v }}</p>
{{ end }}
Result:
<p>0: foo</p>
<p>1: bar</p>
<p>2: baz</p>
Maps
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 }}
Result:
<p>John is 30</p>
<p>Will is 28</p>
<p>Joey is 24</p>
Page collections
Template:
{{ range where site.RegularPages "Type" "articles" }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
Result:
<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>
Break
Use the break
statement to stop the innermost iteration and bypass all remaining iterations.
Template:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
{{ if eq . "bar" }}
{{ break }}
{{ end }}
<p>{{ . }}</p>
{{ end }}
Result:
<p>foo</p>
Continue
Use the continue
statement to stop the innermost iteration and continue to the next iteration.
Template:
{{ $s := slice "foo" "bar" "baz" }}
{{ range $s }}
{{ if eq . "bar" }}
{{ continue }}
{{ end }}
<p>{{ . }}</p>
{{ end }}
Result:
<p>foo</p>
<p>baz</p>