mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'
This commit is contained in:
@@ -3,13 +3,11 @@ title: collections.After
|
||||
description: Slices an array to the items after the Nth item.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [after]
|
||||
related:
|
||||
- functions/collections/First
|
||||
- functions/collections/Last
|
||||
returnType: any
|
||||
signatures: [collections.After INDEX COLLECTION]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [after]
|
||||
returnType: any
|
||||
signatures: [collections.After INDEX COLLECTION]
|
||||
aliases: [/functions/after]
|
||||
---
|
||||
|
||||
@@ -40,7 +38,7 @@ You can use `after` in combination with the [`first`] function and Hugo's [power
|
||||
1. The top row is titled "Featured" and shows only the most recently published article (i.e. by `publishdate` in the content files' front matter).
|
||||
1. The second row is titled "Recent Articles" and shows only the 2nd- to 4th-most recently published articles.
|
||||
|
||||
{{< code file=layouts/section/articles.html >}}
|
||||
```go-html-template {file="layouts/section/articles.html"}
|
||||
{{ define "main" }}
|
||||
<section class="row featured-article">
|
||||
<h2>Featured Article</h2>
|
||||
@@ -63,7 +61,7 @@ You can use `after` in combination with the [`first`] function and Hugo's [power
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ end }}
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
[`first`]: /functions/collections/first/
|
||||
[`slice`]: /functions/collections/slice/
|
||||
|
@@ -3,14 +3,13 @@ title: collections.Append
|
||||
description: Appends one or more elements to a slice and returns the resulting slice.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [append]
|
||||
related:
|
||||
- functions/collections/Merge
|
||||
returnType: any
|
||||
signatures:
|
||||
- collections.Append ELEMENT [ELEMENT...] COLLECTION
|
||||
- collections.Append COLLECTION1 COLLECTION2
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [append]
|
||||
returnType: any
|
||||
signatures:
|
||||
- collections.Append ELEMENT [ELEMENT...] COLLECTION
|
||||
- collections.Append COLLECTION1 COLLECTION2
|
||||
aliases: [/functions/append]
|
||||
---
|
||||
|
||||
|
@@ -3,11 +3,11 @@ title: collections.Apply
|
||||
description: Returns a new collection with each element transformed by the given function.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [apply]
|
||||
related: []
|
||||
returnType: '[]any'
|
||||
signatures: [collections.Apply COLLECTION FUNCTION PARAM...]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [apply]
|
||||
returnType: '[]any'
|
||||
signatures: [collections.Apply COLLECTION FUNCTION PARAM...]
|
||||
aliases: [/functions/apply]
|
||||
---
|
||||
|
||||
|
@@ -3,14 +3,11 @@ title: collections.Complement
|
||||
description: Returns the elements of the last collection that are not in any of the others.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [complement]
|
||||
related:
|
||||
- functions/collections/Intersect
|
||||
- functions/collections/SymDiff
|
||||
- functions/collections/Union
|
||||
returnType: any
|
||||
signatures: ['collections.Complement COLLECTION [COLLECTION...]']
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [complement]
|
||||
returnType: any
|
||||
signatures: ['collections.Complement COLLECTION [COLLECTION...]']
|
||||
aliases: [/functions/complement]
|
||||
---
|
||||
|
||||
@@ -24,11 +21,8 @@ To find the elements within `$c3` that do not exist in `$c1` or `$c2`:
|
||||
{{ complement $c1 $c2 $c3 }} → [1 2]
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
Make your code simpler to understand by using a [chained pipeline]:
|
||||
|
||||
[chained pipeline]: https://pkg.go.dev/text/template#hdr-Pipelines
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Make your code simpler to understand by using a [chained pipeline]:
|
||||
|
||||
```go-html-template
|
||||
{{ $c3 | complement $c1 $c2 }} → [1 2]
|
||||
@@ -55,11 +49,8 @@ To list everything except blog articles (`blog`) and frequently asked questions
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
Although the example above demonstrates the `complement` function, you could use the [`where`] function as well:
|
||||
|
||||
[`where`]: /functions/collections/where/
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Although the example above demonstrates the `complement` function, you could use the [`where`] function as well:
|
||||
|
||||
```go-html-template
|
||||
{{ range where site.RegularPages "Type" "not in" (slice "blog" "faqs") }}
|
||||
@@ -77,4 +68,6 @@ In this example we use the `complement` function to remove [stop words] from a s
|
||||
{{ delimit $filtered " " }} → The quick brown fox jumps lazy dog
|
||||
```
|
||||
|
||||
[`where`]: /functions/collections/where/
|
||||
[chained pipeline]: https://pkg.go.dev/text/template#hdr-Pipelines
|
||||
[stop words]: https://en.wikipedia.org/wiki/Stop_word
|
||||
|
@@ -3,12 +3,11 @@ title: collections.Delimit
|
||||
description: Loops through any array, slice, or map and returns a string of all the values separated by a delimiter.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [delimit]
|
||||
related:
|
||||
- functions/strings/Split
|
||||
returnType: string
|
||||
signatures: ['collections.Delimit COLLECTION DELIMITER [LAST]']
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [delimit]
|
||||
returnType: string
|
||||
signatures: ['collections.Delimit COLLECTION DELIMITER [LAST]']
|
||||
aliases: [/functions/delimit]
|
||||
---
|
||||
|
||||
@@ -22,9 +21,8 @@ Delimit a slice:
|
||||
|
||||
Delimit a map:
|
||||
|
||||
{{% note %}}
|
||||
The `delimit` function sorts maps by key, returning the values.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> The `delimit` function sorts maps by key, returning the values.
|
||||
|
||||
```go-html-template
|
||||
{{ $m := dict "b" 2 "a" 1 "c" 3 }}
|
||||
|
@@ -3,12 +3,11 @@ title: collections.Dictionary
|
||||
description: Returns a map composed of the given key-value pairs.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [dict]
|
||||
related:
|
||||
- functions/collections/Slice
|
||||
returnType: map[string]any
|
||||
signatures: ['collections.Dictionary [VALUE...]']
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [dict]
|
||||
returnType: map[string]any
|
||||
signatures: ['collections.Dictionary [VALUE...]']
|
||||
aliases: [/functions/dict]
|
||||
---
|
||||
|
||||
|
@@ -3,14 +3,11 @@ title: collections.First
|
||||
description: Returns the given collection, limited to the first N elements.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [first]
|
||||
related:
|
||||
- functions/collections/After
|
||||
- functions/collections/Last
|
||||
- methods/pages/Limit
|
||||
returnType: any
|
||||
signatures: [collections.First N COLLECTION]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [first]
|
||||
returnType: any
|
||||
signatures: [collections.First N COLLECTION]
|
||||
aliases: [/functions/first]
|
||||
---
|
||||
|
||||
@@ -23,7 +20,7 @@ aliases: [/functions/first]
|
||||
Set `N` to zero to return an empty collection.
|
||||
|
||||
```go-html-template
|
||||
{{ $emptyPageCollection := first 0 .Pages}}
|
||||
{{ $emptyPageCollection := first 0 .Pages }}
|
||||
```
|
||||
|
||||
Use `first` and [`where`] together.
|
||||
|
@@ -3,11 +3,11 @@ title: collections.Group
|
||||
description: Groups the given page collection by the given key.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [group]
|
||||
related: []
|
||||
returnType: any
|
||||
signatures: [collections.Group KEY PAGES]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [group]
|
||||
returnType: any
|
||||
signatures: [collections.Group KEY PAGES]
|
||||
aliases: [/functions/group]
|
||||
---
|
||||
|
||||
|
@@ -3,16 +3,11 @@ title: collections.In
|
||||
description: Reports whether the given value is a member of the given set.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [in]
|
||||
related:
|
||||
- functions/strings/Contains
|
||||
- functions/strings/ContainsAny
|
||||
- functions/strings/ContainsNonSpace
|
||||
- functions/strings/HasPrefix
|
||||
- functions/strings/HasSuffix
|
||||
returnType: bool
|
||||
signatures: [collections.In SET VALUE]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [in]
|
||||
returnType: bool
|
||||
signatures: [collections.In SET VALUE]
|
||||
aliases: [/functions/in]
|
||||
---
|
||||
|
||||
|
@@ -3,12 +3,11 @@ title: collections.Index
|
||||
description: Returns the object, element, or value associated with the given key or keys.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [index]
|
||||
related: []
|
||||
returnType: any
|
||||
signatures:
|
||||
- collections.Index COLLECTION KEY...
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [index]
|
||||
returnType: any
|
||||
signatures: [collections.Index COLLECTION KEY...]
|
||||
aliases: [/functions/index,/functions/index-function]
|
||||
---
|
||||
|
||||
|
@@ -3,14 +3,11 @@ title: collections.Intersect
|
||||
description: Returns the common elements of two arrays or slices, in the same order as the first array.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [intersect]
|
||||
related:
|
||||
- functions/collections/Complement
|
||||
- functions/collections/SymDiff
|
||||
- functions/collections/Union
|
||||
returnType: any
|
||||
signatures: [collections.Intersect SET1 SET2]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [intersect]
|
||||
returnType: any
|
||||
signatures: [collections.Intersect SET1 SET2]
|
||||
aliases: [/functions/intersect]
|
||||
---
|
||||
|
||||
|
@@ -3,13 +3,11 @@ title: collections.IsSet
|
||||
description: Reports whether the key exists within the collection.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [isset]
|
||||
related:
|
||||
- functions/go-template/if
|
||||
- functions/go-template/with
|
||||
returnType: bool
|
||||
signatures: [collections.IsSet COLLECTION KEY]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [isset]
|
||||
returnType: bool
|
||||
signatures: [collections.IsSet COLLECTION KEY]
|
||||
aliases: [/functions/isset]
|
||||
---
|
||||
|
||||
@@ -40,6 +38,5 @@ But if the value of `showHeroImage` is `false`, we can't use either `if` or `wit
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
When using the `isset` function you must reference the key using lower case. See the previous example.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> When using the `isset` function you must reference the key using lower case. See the previous example.
|
||||
|
@@ -3,12 +3,11 @@ title: collections.KeyVals
|
||||
description: Returns a KeyVals struct.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [keyVals]
|
||||
related:
|
||||
- methods/pages/Related
|
||||
returnType: types.KeyValues
|
||||
signatures: [collections.KeyVals KEY VALUE...]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [keyVals]
|
||||
returnType: types.KeyValues
|
||||
signatures: [collections.KeyVals KEY VALUE...]
|
||||
aliases: [/functions/keyvals]
|
||||
---
|
||||
|
||||
@@ -16,7 +15,7 @@ The primary application for this function is the definition of the `namedSlices`
|
||||
|
||||
[`Related`]: /methods/pages/related/
|
||||
|
||||
See [related content](/content-management/related).
|
||||
See [related content](/content-management/related-content/).
|
||||
|
||||
```go-html-template
|
||||
{{ $kv := keyVals "foo" "a" "b" "c" }}
|
||||
|
@@ -3,13 +3,11 @@ title: collections.Last
|
||||
description: Returns the given collection, limited to the last N elements.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [last]
|
||||
related:
|
||||
- functions/collections/After
|
||||
- functions/collections/First
|
||||
returnType: any
|
||||
signatures: [collections.Last N COLLECTION]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [last]
|
||||
returnType: any
|
||||
signatures: [collections.Last N COLLECTION]
|
||||
aliases: [/functions/last]
|
||||
---
|
||||
|
||||
@@ -22,7 +20,7 @@ aliases: [/functions/last]
|
||||
Set `N` to zero to return an empty collection.
|
||||
|
||||
```go-html-template
|
||||
{{ $emptyPageCollection := last 0 .Pages}}
|
||||
{{ $emptyPageCollection := last 0 .Pages }}
|
||||
```
|
||||
|
||||
Use `last` and [`where`] together.
|
||||
|
@@ -3,12 +3,11 @@ title: collections.Merge
|
||||
description: Returns the result of merging two or more maps.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [merge]
|
||||
related:
|
||||
- functions/collections/Append
|
||||
returnType: any
|
||||
signatures: [collections.Merge MAP MAP...]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [merge]
|
||||
returnType: any
|
||||
signatures: [collections.Merge MAP MAP...]
|
||||
aliases: [/functions/merge]
|
||||
---
|
||||
|
||||
@@ -64,6 +63,5 @@ Example 4
|
||||
{{ $merged.z.a }} → huey
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
Regardless of depth, merging only applies to maps. For slices, use [append](/functions/collections/append).
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Regardless of depth, merging only applies to maps. For slices, use [append](/functions/collections/append).
|
||||
|
@@ -3,23 +3,18 @@ title: collections.NewScratch
|
||||
description: Returns a locally scoped "scratch pad" to store and manipulate data.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [newScratch]
|
||||
related:
|
||||
- methods/page/Store
|
||||
- methods/site/Store
|
||||
- methods/shortcode/Store
|
||||
- functions/hugo/Store
|
||||
returnType: maps.Scratch
|
||||
signatures: [collections.NewScratch ]
|
||||
toc: true
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [newScratch]
|
||||
returnType: maps.Scratch
|
||||
signatures: [collections.NewScratch ]
|
||||
---
|
||||
|
||||
Use the `collections.NewScratch` function to create a locally scoped [scratch pad](g) to store and manipulate data. To create a scratch pad with a different [scope](g), refer to the [scope](#scope) section below.
|
||||
|
||||
## Methods
|
||||
|
||||
###### Set
|
||||
### Set
|
||||
|
||||
Sets the value of the given key.
|
||||
|
||||
@@ -28,7 +23,7 @@ Sets the value of the given key.
|
||||
{{ $s.Set "greeting" "Hello" }}
|
||||
```
|
||||
|
||||
###### Get
|
||||
### Get
|
||||
|
||||
Gets the value of the given key.
|
||||
|
||||
@@ -38,7 +33,7 @@ Gets the value of the given key.
|
||||
{{ $s.Get "greeting" }} → Hello
|
||||
```
|
||||
|
||||
###### Add
|
||||
### Add
|
||||
|
||||
Adds the given value to existing value(s) of the given key.
|
||||
|
||||
@@ -65,7 +60,7 @@ For single values, `Add` accepts values that support Go's `+` operator. If the f
|
||||
{{ $s.Get "greetings" }} → [Hello Welcome Cheers]
|
||||
```
|
||||
|
||||
###### SetInMap
|
||||
### SetInMap
|
||||
|
||||
Takes a `key`, `mapKey` and `value` and adds a map of `mapKey` and `value` to the given `key`.
|
||||
|
||||
@@ -76,7 +71,7 @@ Takes a `key`, `mapKey` and `value` and adds a map of `mapKey` and `value` to th
|
||||
{{ $s.Get "greetings" }} → map[english:Hello french:Bonjour]
|
||||
```
|
||||
|
||||
###### DeleteInMap
|
||||
### DeleteInMap
|
||||
|
||||
Takes a `key` and `mapKey` and removes the map of `mapKey` from the given `key`.
|
||||
|
||||
@@ -88,7 +83,7 @@ Takes a `key` and `mapKey` and removes the map of `mapKey` from the given `key`.
|
||||
{{ $s.Get "greetings" }} → map[french:Bonjour]
|
||||
```
|
||||
|
||||
###### GetSortedMapValues
|
||||
### GetSortedMapValues
|
||||
|
||||
Returns an array of values from `key` sorted by `mapKey`.
|
||||
|
||||
@@ -99,7 +94,7 @@ Returns an array of values from `key` sorted by `mapKey`.
|
||||
{{ $s.GetSortedMapValues "greetings" }} → [Hello Bonjour]
|
||||
```
|
||||
|
||||
###### Delete
|
||||
### Delete
|
||||
|
||||
Removes the given key.
|
||||
|
||||
@@ -109,9 +104,9 @@ Removes the given key.
|
||||
{{ $s.Delete "greeting" }}
|
||||
```
|
||||
|
||||
###### Values
|
||||
### Values
|
||||
|
||||
Returns the raw backing map. Do not use with `Scratch` or `Store` methods on a `Page` object due to concurrency issues.
|
||||
Returns the raw backing map. Do not use with `Store` methods on a `Page` object due to concurrency issues.
|
||||
|
||||
```go-html-template
|
||||
{{ $s := newScratch }}
|
||||
|
@@ -3,13 +3,11 @@ title: collections.Querify
|
||||
description: Returns a URL query string composed of the given key-value pairs, encoded and sorted by key.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [querify]
|
||||
related:
|
||||
- functions/go-template/urlquery.md
|
||||
returnType: string
|
||||
signatures:
|
||||
- collections.Querify [VALUE...]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [querify]
|
||||
returnType: string
|
||||
signatures: ['collections.Querify [VALUE...]']
|
||||
aliases: [/functions/querify]
|
||||
---
|
||||
|
||||
|
@@ -3,14 +3,11 @@ title: collections.Reverse
|
||||
description: Reverses the order of a collection.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/collections/Sort
|
||||
- functions/collections/Shuffle
|
||||
- functions/collections/Uniq
|
||||
returnType: any
|
||||
signatures: [collections.Reverse COLLECTION]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: []
|
||||
returnType: any
|
||||
signatures: [collections.Reverse COLLECTION]
|
||||
aliases: [/functions/collections.reverse]
|
||||
---
|
||||
|
||||
|
@@ -3,14 +3,14 @@ title: collections.Seq
|
||||
description: Returns a slice of integers.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [seq]
|
||||
related: []
|
||||
returnType: '[]int'
|
||||
signatures:
|
||||
- collections.Seq LAST
|
||||
- collections.Seq FIRST LAST
|
||||
- collections.Seq FIRST INCREMENT LAST
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [seq]
|
||||
returnType: '[]int'
|
||||
signatures:
|
||||
- collections.Seq LAST
|
||||
- collections.Seq FIRST LAST
|
||||
- collections.Seq FIRST INCREMENT LAST
|
||||
aliases: [/functions/seq]
|
||||
---
|
||||
|
||||
@@ -31,6 +31,5 @@ A contrived example of iterating over a sequence of integers:
|
||||
{{ $product }} → 24
|
||||
```
|
||||
|
||||
{{% note %}}
|
||||
The slice created by the `seq` function is limited to 2000 elements.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> The slice created by the `seq` function is limited to 2000 elements.
|
||||
|
@@ -3,14 +3,11 @@ title: collections.Shuffle
|
||||
description: Returns a random permutation of a given array or slice.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [shuffle]
|
||||
related:
|
||||
- functions/collections/Reverse
|
||||
- functions/collections/Sort
|
||||
- functions/collections/Uniq
|
||||
returnType: any
|
||||
signatures: [collections.Shuffle COLLECTION]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [shuffle]
|
||||
returnType: any
|
||||
signatures: [collections.Shuffle COLLECTION]
|
||||
aliases: [/functions/shuffle]
|
||||
---
|
||||
|
||||
|
@@ -3,12 +3,11 @@ title: collections.Slice
|
||||
description: Returns a slice composed of the given values.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [slice]
|
||||
related:
|
||||
- functions/collections/Dictionary
|
||||
returnType: any
|
||||
signatures: ['collections.Slice [VALUE...]']
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [slice]
|
||||
returnType: any
|
||||
signatures: ['collections.Slice [VALUE...]']
|
||||
aliases: [/functions/slice]
|
||||
---
|
||||
|
||||
|
@@ -3,15 +3,11 @@ title: collections.Sort
|
||||
description: Sorts slices, maps, and page collections.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [sort]
|
||||
related:
|
||||
- functions/collections/Reverse
|
||||
- functions/collections/Shuffle
|
||||
- functions/collections/Uniq
|
||||
returnType: any
|
||||
signatures: ['collections.Sort COLLECTION [KEY] [ORDER]']
|
||||
toc: true
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [sort]
|
||||
returnType: any
|
||||
signatures: ['collections.Sort COLLECTION [KEY] [ORDER]']
|
||||
aliases: [/functions/sort]
|
||||
---
|
||||
|
||||
@@ -65,9 +61,8 @@ firstName = "Jean"
|
||||
lastName = "Valjean"
|
||||
{{< /code-toggle >}}
|
||||
|
||||
{{% note %}}
|
||||
When sorting maps, the `KEY` argument must be lowercase.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> When sorting maps, the `KEY` argument must be lowercase.
|
||||
|
||||
### Ascending order {#map-ascending-order}
|
||||
|
||||
@@ -141,11 +136,8 @@ After sorting:
|
||||
|
||||
## Sort a page collection
|
||||
|
||||
{{% note %}}
|
||||
Although you can use the `sort` function to sort a page collection, Hugo provides [sorting and grouping methods] as well.
|
||||
|
||||
[sorting and grouping methods]: /methods/pages/
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Although you can use the `sort` function to sort a page collection, Hugo provides [sorting and grouping methods] as well.
|
||||
|
||||
In this contrived example, sort the site's regular pages by `.Type` in descending order:
|
||||
|
||||
@@ -154,3 +146,5 @@ In this contrived example, sort the site's regular pages by `.Type` in descendin
|
||||
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
|
||||
{{ end }}
|
||||
```
|
||||
|
||||
[sorting and grouping methods]: /methods/pages/
|
||||
|
@@ -3,15 +3,11 @@ title: collections.SymDiff
|
||||
description: Returns the symmetric difference of two collections.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [symdiff]
|
||||
related:
|
||||
- functions/collections/Complement
|
||||
- functions/collections/Intersect
|
||||
- functions/collections/SymDiff
|
||||
- functions/collections/Union
|
||||
returnType: any
|
||||
signatures: [COLLECTION | collections.SymDiff COLLECTION]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [symdiff]
|
||||
returnType: any
|
||||
signatures: [COLLECTION | collections.SymDiff COLLECTION]
|
||||
aliases: [/functions/symdiff]
|
||||
---
|
||||
|
||||
|
@@ -3,32 +3,24 @@ title: collections.Union
|
||||
description: Given two arrays or slices, returns a new array that contains the elements that belong to either or both arrays/slices.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [union]
|
||||
related:
|
||||
- functions/collections/Complement
|
||||
- functions/collections/Intersect
|
||||
- functions/collections/SymDiff
|
||||
- functions/collections/Union
|
||||
returnType: any
|
||||
signatures: [collections.Union SET1 SET2]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [union]
|
||||
returnType: any
|
||||
signatures: [collections.Union SET1 SET2]
|
||||
aliases: [/functions/union]
|
||||
---
|
||||
|
||||
Given two arrays (or slices) A and B, this function will return a new array that contains the elements or objects that belong to either A or to B or to both.
|
||||
|
||||
```go-html-template
|
||||
{{ union (slice 1 2 3) (slice 3 4 5) }}
|
||||
<!-- returns [1 2 3 4 5] -->
|
||||
{{ union (slice 1 2 3) (slice 3 4 5) }} → [1 2 3 4 5]
|
||||
|
||||
{{ union (slice 1 2 3) nil }}
|
||||
<!-- returns [1 2 3] -->
|
||||
{{ union (slice 1 2 3) nil }} → [1 2 3]
|
||||
|
||||
{{ union nil (slice 1 2 3) }}
|
||||
<!-- returns [1 2 3] -->
|
||||
{{ union nil (slice 1 2 3) }} → [1 2 3]
|
||||
|
||||
{{ union nil nil }}
|
||||
<!-- returns an error because both arrays/slices have to be of the same type -->
|
||||
{{ union nil nil }} → []
|
||||
```
|
||||
|
||||
## OR filter in where query
|
||||
|
@@ -3,15 +3,11 @@ title: collections.Uniq
|
||||
description: Returns the given collection, removing duplicate elements.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [uniq]
|
||||
related:
|
||||
- functions/collections/Reverse
|
||||
- functions/collections/Shuffle
|
||||
- functions/collections/Sort
|
||||
- functions/collections/Uniq
|
||||
returnType: any
|
||||
signatures: [collections.Uniq COLLECTION]
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [uniq]
|
||||
returnType: any
|
||||
signatures: [collections.Uniq COLLECTION]
|
||||
aliases: [/functions/uniq]
|
||||
---
|
||||
|
||||
|
@@ -3,12 +3,11 @@ title: collections.Where
|
||||
description: Returns the given collection, removing elements that do not satisfy the comparison condition.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [where]
|
||||
related: []
|
||||
returnType: any
|
||||
signatures: ['collections.Where COLLECTION KEY [OPERATOR] VALUE']
|
||||
toc: true
|
||||
params:
|
||||
functions_and_methods:
|
||||
aliases: [where]
|
||||
returnType: any
|
||||
signatures: ['collections.Where COLLECTION KEY [OPERATOR] VALUE']
|
||||
aliases: [/functions/where]
|
||||
---
|
||||
|
||||
@@ -87,12 +86,12 @@ Use any of the following logical operators:
|
||||
`intersect`
|
||||
: (`bool`) Reports whether the given field value (a slice) contains one or more elements in common with `VALUE`. See [details](/functions/collections/intersect).
|
||||
|
||||
`like` {{< new-in 0.116.0 />}}
|
||||
: (`bool`) Reports whether the given field value matches the regular expression specified in `VALUE`. Use the `like` operator to compare `string` values. The `like` operator returns `false` when comparing other data types to the regular expression.
|
||||
`like`
|
||||
: {{< new-in 0.116.0 />}}
|
||||
: (`bool`) Reports whether the given field value matches the [regular expression](g) specified in `VALUE`. Use the `like` operator to compare `string` values. The `like` operator returns `false` when comparing other data types to the regular expression.
|
||||
|
||||
{{% note %}}
|
||||
The examples below perform comparisons within a page collection, but the same comparisons are applicable to a slice of maps.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> The examples below perform comparisons within a page collection, but the same comparisons are applicable to a slice of maps.
|
||||
|
||||
## String comparison
|
||||
|
||||
@@ -155,7 +154,7 @@ To return a collection of pages where the "color" page parameter is neither "red
|
||||
|
||||
## Intersection comparison
|
||||
|
||||
Compare a [`slice`] to a [`slice`], returning collection elements with common values. This is frequently used when comparing taxonomy terms.
|
||||
Compare a `slice` to a `slice`, returning collection elements with common values. This is frequently used when comparing taxonomy terms.
|
||||
|
||||
For example, to return a collection of pages where any of the terms in the "genres" taxonomy are "suspense" or "romance":
|
||||
|
||||
@@ -176,11 +175,10 @@ To return a collection of pages where the "author" page parameter begins with ei
|
||||
{{ $pages := where .Site.RegularPages "Params.author" "like" `(?i)^victor` }}
|
||||
```
|
||||
|
||||
{{% include "functions/_common/regular-expressions.md" %}}
|
||||
{{% include "/_common/functions/regular-expressions.md" %}}
|
||||
|
||||
{{% note %}}
|
||||
Use the `like` operator to compare string values. Comparing other data types will result in an empty collection.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Use the `like` operator to compare string values. Comparing other data types will result in an empty collection.
|
||||
|
||||
## Date comparison
|
||||
|
||||
@@ -188,12 +186,6 @@ Use the `like` operator to compare string values. Comparing other data types wil
|
||||
|
||||
There are four predefined front matter dates: [`date`], [`publishDate`], [`lastmod`], and [`expiryDate`]. Regardless of the front matter data format (TOML, YAML, or JSON) these are [`time.Time`] values, allowing precise comparisons.
|
||||
|
||||
[`date`]: /methods/page/date/
|
||||
[`publishdate`]: /methods/page/publishdate/
|
||||
[`lastmod`]: /methods/page/lastmod/
|
||||
[`expirydate`]: /methods/page/expirydate/
|
||||
[`time.Time`]: https://pkg.go.dev/time#Time
|
||||
|
||||
For example, to return a collection of pages that were created before the current year:
|
||||
|
||||
```go-html-template
|
||||
@@ -205,20 +197,19 @@ For example, to return a collection of pages that were created before the curren
|
||||
|
||||
With custom front matter dates, the comparison depends on the front matter data format (TOML, YAML, or JSON).
|
||||
|
||||
{{% note %}}
|
||||
Using TOML for pages with custom front matter dates enables precise date comparisons.
|
||||
{{% /note %}}
|
||||
> [!note]
|
||||
> Using TOML for pages with custom front matter dates enables precise date comparisons.
|
||||
|
||||
With TOML, date values are first-class citizens. TOML has a date data type while JSON and YAML do not. If you quote a TOML date, it is a string. If you do not quote a TOML date value, it is [`time.Time`] value, enabling precise comparisons.
|
||||
|
||||
In the TOML example below, note that the event date is not quoted.
|
||||
|
||||
{{< code file="content/events/2024-user-conference.md" >}}
|
||||
```text {file="content/events/2024-user-conference.md"}
|
||||
+++
|
||||
title = '2024 User Conference"
|
||||
eventDate = 2024-04-01
|
||||
+++
|
||||
{{< /code >}}
|
||||
```
|
||||
|
||||
To return a collection of future events:
|
||||
|
||||
@@ -272,8 +263,6 @@ These are equivalent:
|
||||
|
||||
Useful for theme authors, avoid hardcoding section names by using the `where` function with the [`MainSections`] method on a `Site` object.
|
||||
|
||||
[`MainSections`]: /methods/site/mainsections/
|
||||
|
||||
```go-html-template
|
||||
{{ $pages := where .Site.RegularPages "Section" "in" .Site.MainSections }}
|
||||
```
|
||||
@@ -281,11 +270,10 @@ Useful for theme authors, avoid hardcoding section names by using the `where` fu
|
||||
With this construct, a theme author can instruct users to specify their main sections in the site configuration:
|
||||
|
||||
{{< code-toggle file=hugo >}}
|
||||
[params]
|
||||
mainSections = ['blog','galleries']
|
||||
{{< /code-toggle >}}
|
||||
|
||||
If `params.mainSections` is not defined in the site configuration, the `MainSections` method returns a slice with one element---the top level section with the most pages.
|
||||
If `mainSections` is not defined in the site configuration, the `MainSections` method returns a slice with one element---the top-level section with the most pages.
|
||||
|
||||
## Boolean/undefined comparison
|
||||
|
||||
@@ -387,13 +375,11 @@ To exclude a page with an undefined field from a boolean _inequality_ test:
|
||||
1. Create a collection using a nil comparison
|
||||
1. Subtract the second collection from the first collection using the [`collections.Complement`] function.
|
||||
|
||||
[`collections.Complement`]: /functions/collections/complement/
|
||||
|
||||
This template:
|
||||
|
||||
```go-html-template
|
||||
{{ $p1 := where .Site.RegularPages "Params.exclude" "ne" true }}
|
||||
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil }}
|
||||
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil }}
|
||||
<ul>
|
||||
{{ range $p1 | complement $p2 }}
|
||||
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
|
||||
@@ -413,7 +399,7 @@ This template:
|
||||
|
||||
```go-html-template
|
||||
{{ $p1 := where .Site.RegularPages "Params.exclude" "ne" false }}
|
||||
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil }}
|
||||
{{ $p2 := where .Site.RegularPages "Params.exclude" "eq" nil }}
|
||||
<ul>
|
||||
{{ range $p1 | complement $p2 }}
|
||||
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
|
||||
@@ -428,3 +414,9 @@ Is rendered to:
|
||||
<li><a href="/posts/post-1/">Post 2</a></li>
|
||||
</ul>
|
||||
```
|
||||
|
||||
[`collections.Complement`]: /functions/collections/complement/
|
||||
[`date`]: /methods/page/date/
|
||||
[`lastmod`]: /methods/page/lastmod/
|
||||
[`MainSections`]: /methods/site/mainsections/
|
||||
[`time.Time`]: https://pkg.go.dev/time#Time
|
||||
|
@@ -1,12 +1,7 @@
|
||||
---
|
||||
title: Collections functions
|
||||
linkTitle: collections
|
||||
description: Template functions to work with arrays, slices, maps, and page collections.
|
||||
description: Use these functions to work with arrays, slices, maps, and page collections.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
---
|
||||
|
||||
Use these functions to work with arrays, slices, maps, and page collections.
|
||||
|
Reference in New Issue
Block a user