--- title: range description: Iterates over slices, maps, and page collections. categories: [functions] keywords: [] menu: docs: parent: functions function: aliases: [] returnType: signatures: [range COLLECTION] relatedFunctions: - with - range aliases: [/functions/range] toc: true --- {{% readfile file="/functions/_common/go-template-functions.md" %}} ## Slices Template: ```go-html-template {{ $s := slice "foo" "bar" "baz" }} {{ range $s }}
{{ . }}
{{ end }} ``` Result: ```htmlfoo
bar
baz
``` Template: ```go-html-template {{ $s := slice "foo" "bar" "baz" }} {{ range $v := $s }}{{ $v }}
{{ end }} ``` Result: ```htmlfoo
bar
baz
``` Template: ```go-html-template {{ $s := slice "foo" "bar" "baz" }} {{ range $k, $v := $s }}{{ $k }}: {{ $v }}
{{ end }} ``` Result: ```html0: foo
1: bar
2: baz
``` ## Maps Template: ```go-html-template {{ $m := slice (dict "name" "John" "age" 30) (dict "name" "Will" "age" 28) (dict "name" "Joey" "age" 24) }} {{ range $m }}{{ .name }} is {{ .age }}
{{ end }} ``` Result: ```htmlJohn is 30
Will is 28
Joey is 24
``` ## Page collections Template: ```go-html-template {{ range where site.RegularPages "Type" "articles" }}{{ . }}
{{ end }} ``` Result: ```htmlfoo
``` ## Continue Use the `continue` statement to stop the innermost iteration and continue to the next iteration. Template: ```go-html-template {{ $s := slice "foo" "bar" "baz" }} {{ range $s }} {{ if eq . "bar" }} {{ continue }} {{ end }}{{ . }}
{{ end }} ``` Result: ```htmlfoo
baz
```