Gopher

collections

Collections is Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

collections.After  

(index any, seq any) → any

After returns all the items after the first N in a rangeable list.

Aliases: after

collections.Append  

(args …any) → any

Append appends the arguments up to the last one to the slice in the last argument. This construct allows template constructs like this: {{ $pages = $pages | append $p2 $p1 }} Note that with 2 arguments where both are slices of the same type, the first slice will be appended to the second: {{ $pages = $pages | append .Site.RegularPages }}

Aliases: append

collections.Apply  

(seq any, fname string, args …any) → any

Apply takes a map, array, or slice and returns a new slice with the function fname applied over it.

Aliases: apply

collections.Complement  

(seqs …any) → any

Complement gives the elements in the last element of seqs that are not in any of the others. All elements of seqs must be slices or arrays of comparable types.

The reasoning behind this rather clumsy API is so we can do this in the templates: {{ $c := .Pages | complement $last4 }}

Aliases: complement

Examples

{{ slice "a" "b" "c" "d" "e" "f" | complement (slice "b" "c") (slice "d" "e")  }}
[a f]

collections.Delimit  

(seq any, last …any) → template.HTML

Delimit takes a given sequence and returns a delimited HTML string. If last is passed to the function, it will be used as the final delimiter.

Aliases: delimit

Examples

{{ delimit (slice "A" "B" "C") ", " " and " }}
A, B and C

collections.Dictionary  

(values …any) → map[string]any

Dictionary creates a map[string]interface{} from the given parameters by walking the parameters and treating them as key-value pairs. The number of parameters must be even. The keys can be string slices, which will create the needed nested structure.

Aliases: dict

collections.EchoParam  

(a any) → any

EchoParam returns a given value if it is set; otherwise, it returns an empty string.

Aliases: echoParam

Examples

{{ echoParam .Params "langCode" }}
en

collections.First  

(limit any, seq any) → any

First returns the first N items in a rangeable list.

Aliases: first

collections.Group  

(key any, items any) → any

Group groups a set of elements by the given key. This is currently only supported for Pages.

Aliases: group

collections.In  

(l any, v any) → bool

In returns whether v is in the set l. l may be an array or slice.

Aliases: in

Examples

{{ if in "this string contains a substring" "substring" }}Substring found!{{ end }}
Substring found!

collections.Index  

(item any, args …any) → any

Index returns the result of indexing its first argument by the following arguments. Thus “index x 1 2 3” is, in Go syntax, x[1][2][3]. Each indexed item must be a map, slice, or array.

Copied from Go stdlib src/text/template/funcs.go.

We deviate from the stdlib due to https://github.com/golang/go/issues/14751.

TODO(moorereason): merge upstream changes.

Aliases: index

collections.Intersect  

(l1 any) → any

Intersect returns the common elements in the given sets, l1 and l2. l1 and l2 must be of the same type and may be either arrays or slices.

Aliases: intersect

collections.IsSet  

(a any, key any) → bool

IsSet returns whether a given array, channel, slice, or map has a key defined.

Aliases: isSet, isset

collections.KeyVals  

(key any, vals …any) → KeyValues

KeyVals creates a key and values wrapper.

Aliases: keyVals

Examples

{{ keyVals "key" "a" "b" }}
key: [a b]

collections.Last  

(limit any, seq any) → any

Last returns the last N items in a rangeable list.

Aliases: last

collections.Merge  

(params …any) → any

Merge creates a copy of the final parameter and merges the preceding parameters into it in reverse order. Currently only maps are supported. Key handling is case insensitive.

Aliases: merge

Examples

{{ dict "title" "Hugo Rocks!" | collections.Merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") | sort }}
[Yes, Hugo Rocks! Hugo Rocks!]
{{  merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") | sort }}
[Yes, Hugo Rocks! Hugo Rocks!]
{{  merge (dict "title" "Default Title" "description" "Yes, Hugo Rocks!") (dict "title" "Hugo Rocks!") (dict "extra" "For reals!") | sort }}
[Yes, Hugo Rocks! For reals! Hugo Rocks!]

collections.NewScratch  

() → maps.Scratch

NewScratch creates a new Scratch which can be used to store values in a thread safe way.

Aliases: newScratch

Examples

{{ $scratch := newScratch }}{{ $scratch.Add "b" 2 }}{{ $scratch.Add "b" 2 }}{{ $scratch.Get "b" }}
4

collections.Querify  

(params …any) → string

Querify encodes the given parameters in URL-encoded form (“bar=baz&foo=quux”) sorted by key.

Aliases: querify

Examples

{{ (querify "foo" 1 "bar" 2 "baz" "with spaces" "qux" "this&that=those") | safeHTML }}
bar=2&baz=with+spaces&foo=1&qux=this%26that%3Dthose
<a href="https://www.google.com?{{ (querify "q" "test" "page" 3) | safeURL }}">Search</a>
<a href="https://www.google.com?page=3&amp;q=test">Search</a>
{{ slice "foo" 1 "bar" 2 | querify | safeHTML }}
bar=2&foo=1

collections.Reverse  

(slice any) → any

Reverse creates a copy of slice and reverses it.

collections.Seq  

(args …any) → []int

Seq creates a sequence of integers. It’s named and used as GNU’s seq.

Examples: 3 => 1, 2, 3 1 2 4 => 1, 3 -3 => -1, -2, -3 1 4 => 1, 2, 3, 4 1 -2 => 1, 0, -1, -2

Aliases: seq

Examples

{{ seq 3 }}
[1 2 3]

collections.Shuffle  

(seq any) → any

Shuffle returns the given rangeable list in a randomised order.

Aliases: shuffle

collections.Slice  

(args …any) → any

Slice returns a slice of all passed arguments.

Aliases: slice

Examples

{{ slice "B" "C" "A" | sort }}
[A B C]

collections.Sort  

(seq any, args …any) → any

Sort returns a sorted sequence.

Aliases: sort

collections.SymDiff  

(s2 any) → any

SymDiff returns the symmetric difference of s1 and s2. Arguments must be either a slice or an array of comparable types.

Aliases: symdiff

Examples

{{ slice 1 2 3 | symdiff (slice 3 4) }}
[1 2 4]

collections.Union  

(l1 any) → any

Union returns the union of the given sets, l1 and l2. l1 and l2 must be of the same type and may be either arrays or slices. If l1 and l2 aren’t of the same type then l1 will be returned. If either l1 or l2 is nil then the non-nil list will be returned.

Aliases: union

Examples

{{ union (slice 1 2 3) (slice 3 4 5) }}
[1 2 3 4 5]

collections.Uniq  

(seq any) → any

Uniq takes in a slice or array and returns a slice with subsequent duplicate elements removed.

Aliases: uniq

Examples

{{ slice 1 2 3 2 | uniq }}
[1 2 3]

collections.Where  

(seq any, args …any) → any

Where returns a filtered subset of a given data type.

Aliases: where