Documentation ¶
Overview ¶
Package collections provides template functions for manipulating collections such as arrays, maps, and slices.
Index ¶
- type Namespace
- func (ns *Namespace) After(index any, seq any) (any, error)
- func (ns *Namespace) Append(args ...any) (any, error)
- func (ns *Namespace) Apply(ctx context.Context, seq any, fname string, args ...any) (any, error)
- func (ns *Namespace) Complement(seqs ...any) (any, error)
- func (ns *Namespace) Delimit(seq, delimiter any, last ...any) (template.HTML, error)
- func (ns *Namespace) Dictionary(values ...any) (map[string]any, error)
- func (ns *Namespace) EchoParam(a, key any) any
- func (ns *Namespace) First(limit any, seq any) (any, error)
- func (ns *Namespace) Group(key any, items any) (any, error)
- func (ns *Namespace) In(l any, v any) (bool, error)
- func (ns *Namespace) Index(item any, args ...any) (any, error)
- func (ns *Namespace) Intersect(l1, l2 any) (any, error)
- func (ns *Namespace) IsSet(a any, key any) (bool, error)
- func (ns *Namespace) KeyVals(key any, vals ...any) (types.KeyValues, error)
- func (ns *Namespace) Last(limit any, seq any) (any, error)
- func (ns *Namespace) Merge(params ...any) (any, error)
- func (ns *Namespace) NewScratch() *maps.Scratch
- func (ns *Namespace) Querify(params ...any) (string, error)
- func (ns *Namespace) Reverse(slice any) (any, error)
- func (ns *Namespace) Seq(args ...any) ([]int, error)
- func (ns *Namespace) Shuffle(seq any) (any, error)
- func (ns *Namespace) Slice(args ...any) any
- func (ns *Namespace) Sort(seq any, args ...any) (any, error)
- func (ns *Namespace) SymDiff(s2, s1 any) (any, error)
- func (ns *Namespace) Union(l1, l2 any) (any, error)
- func (ns *Namespace) Uniq(seq any) (any, error)
- func (ns *Namespace) Where(seq, key any, args ...any) (any, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Namespace ¶
type Namespace struct {
// contains filtered or unexported fields
}
Namespace provides template functions for the "collections" namespace.
func (*Namespace) Append ¶ added in v0.49.1
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 }}
func (*Namespace) Apply ¶
Apply takes a map, array, or slice and returns a new slice with the function fname applied over it.
func (*Namespace) Complement ¶ added in v0.54.0
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 }}
func (*Namespace) Delimit ¶
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.
func (*Namespace) Dictionary ¶
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.
func (*Namespace) EchoParam ¶
EchoParam returns a given value if it is set; otherwise, it returns an empty string.
func (*Namespace) Group ¶ added in v0.49.1
Group groups a set of elements by the given key. This is currently only supported for Pages.
func (*Namespace) Index ¶
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.
func (*Namespace) Intersect ¶
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.
func (*Namespace) IsSet ¶
IsSet returns whether a given array, channel, slice, or map has a key defined.
func (*Namespace) Merge ¶ added in v0.56.0
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.
func (*Namespace) NewScratch ¶ added in v0.45.1
NewScratch creates a new Scratch which can be used to store values in a thread safe way.
func (*Namespace) Querify ¶
Querify encodes the given parameters in URL-encoded form ("bar=baz&foo=quux") sorted by key.
func (*Namespace) Seq ¶
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
func (*Namespace) SymDiff ¶ added in v0.54.0
SymDiff returns the symmetric difference of s1 and s2. Arguments must be either a slice or an array of comparable types.
func (*Namespace) Union ¶
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.