--- 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: ```html

foo

bar

baz

``` Template: ```go-html-template {{ $s := slice "foo" "bar" "baz" }} {{ range $v := $s }}

{{ $v }}

{{ end }} ``` Result: ```html

foo

bar

baz

``` Template: ```go-html-template {{ $s := slice "foo" "bar" "baz" }} {{ range $k, $v := $s }}

{{ $k }}: {{ $v }}

{{ end }} ``` Result: ```html

0: 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: ```html

John is 30

Will is 28

Joey is 24

``` ## Page collections Template: ```go-html-template {{ range where site.RegularPages "Type" "articles" }}

{{ .LinkTitle }}

{{ end }} ``` Result: ```html

Article 3

Article 2

Article 1

``` ## Break Use the `break` statement to stop the innermost iteration and bypass all remaining iterations. Template: ```go-html-template {{ $s := slice "foo" "bar" "baz" }} {{ range $s }} {{ if eq . "bar" }} {{ break }} {{ end }}

{{ . }}

{{ end }} ``` Result: ```html

foo

``` ## 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: ```html

foo

baz

```