hugo: github.com/gohugoio/hugo/tpl/collections Index | Files

package collections

import "github.com/gohugoio/hugo/tpl/collections"

Package collections provides template functions for manipulating collections such as arrays, maps, and slices.

Index

Package Files

append.go apply.go collections.go complement.go index.go init.go merge.go reflect_helpers.go sort.go symdiff.go where.go

type Namespace Uses

type Namespace struct {
    // contains filtered or unexported fields
}

Namespace provides template functions for the "collections" namespace.

func New Uses

func New(deps *deps.Deps) *Namespace

New returns a new instance of the collections-namespaced template functions.

func (*Namespace) After Uses

func (ns *Namespace) After(index interface{}, seq interface{}) (interface{}, error)

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

func (*Namespace) Append Uses

func (ns *Namespace) Append(args ...interface{}) (interface{}, error)

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 Uses

func (ns *Namespace) Apply(seq interface{}, fname string, args ...interface{}) (interface{}, error)

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

func (*Namespace) Complement Uses

func (ns *Namespace) Complement(seqs ...interface{}) (interface{}, error)

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 Uses

func (ns *Namespace) Delimit(seq, delimiter interface{}, last ...interface{}) (template.HTML, error)

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 Uses

func (ns *Namespace) Dictionary(values ...interface{}) (map[string]interface{}, error)

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 Uses

func (ns *Namespace) EchoParam(a, key interface{}) interface{}

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

func (*Namespace) First Uses

func (ns *Namespace) First(limit interface{}, seq interface{}) (interface{}, error)

First returns the first N items in a rangeable list.

func (*Namespace) Group Uses

func (ns *Namespace) Group(key interface{}, items interface{}) (interface{}, error)

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

func (*Namespace) In Uses

func (ns *Namespace) In(l interface{}, v interface{}) (bool, error)

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

func (*Namespace) Index Uses

func (ns *Namespace) Index(item interface{}, args ...interface{}) (interface{}, error)

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 Uses

func (ns *Namespace) Intersect(l1, l2 interface{}) (interface{}, error)

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 Uses

func (ns *Namespace) IsSet(a interface{}, key interface{}) (bool, error)

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

func (*Namespace) KeyVals Uses

func (ns *Namespace) KeyVals(key interface{}, vals ...interface{}) (types.KeyValues, error)

KeyVals creates a key and values wrapper.

func (*Namespace) Last Uses

func (ns *Namespace) Last(limit interface{}, seq interface{}) (interface{}, error)

Last returns the last N items in a rangeable list.

func (*Namespace) Merge Uses

func (ns *Namespace) Merge(params ...interface{}) (interface{}, error)

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

func (*Namespace) NewScratch Uses

func (ns *Namespace) NewScratch() *maps.Scratch

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

func (*Namespace) Querify Uses

func (ns *Namespace) Querify(params ...interface{}) (string, error)

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

func (*Namespace) Reverse Uses

func (ns *Namespace) Reverse(slice interface{}) (interface{}, error)

Reverse creates a copy of slice and reverses it.

func (*Namespace) Seq Uses

func (ns *Namespace) Seq(args ...interface{}) ([]int, error)

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) Shuffle Uses

func (ns *Namespace) Shuffle(seq interface{}) (interface{}, error)

Shuffle returns the given rangeable list in a randomised order.

func (*Namespace) Slice Uses

func (ns *Namespace) Slice(args ...interface{}) interface{}

Slice returns a slice of all passed arguments.

func (*Namespace) Sort Uses

func (ns *Namespace) Sort(seq interface{}, args ...interface{}) (interface{}, error)

Sort returns a sorted sequence.

func (*Namespace) SymDiff Uses

func (ns *Namespace) SymDiff(s2, s1 interface{}) (interface{}, error)

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

func (*Namespace) Union Uses

func (ns *Namespace) Union(l1, l2 interface{}) (interface{}, error)

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.

func (*Namespace) Uniq Uses

func (ns *Namespace) Uniq(seq interface{}) (interface{}, error)

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

func (*Namespace) Where Uses

func (ns *Namespace) Where(seq, key interface{}, args ...interface{}) (interface{}, error)

Where returns a filtered subset of a given data type.

Package collections imports 21 packages (graph) and is imported by 44 packages. Updated 2020-08-29. Refresh now. Tools for package owners.