mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-21 21:35:28 +02:00
Merge commit '7125ad401ad043e46262afc7eca8dceb6d54bb9e'
This commit is contained in:
@@ -5,16 +5,11 @@ categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [where]
|
||||
related: []
|
||||
returnType: any
|
||||
signatures: ['collections.Where COLLECTION KEY [OPERATOR] VALUE']
|
||||
related:
|
||||
- collections.Dictionary
|
||||
- collections.Group
|
||||
- collections.Index
|
||||
- collections.IsSet
|
||||
- collections.Where
|
||||
aliases: [/functions/where]
|
||||
toc: true
|
||||
aliases: [/functions/where]
|
||||
---
|
||||
|
||||
The `where` function returns the given collection, removing elements that do not satisfy the comparison condition. The comparison condition is comprised of the `KEY`, `OPERATOR`, and `VALUE` arguments:
|
||||
@@ -37,9 +32,10 @@ Hugo will test for equality if you do not provide an `OPERATOR` argument. For ex
|
||||
The where function takes three or four arguments. The `OPERATOR` argument is optional.
|
||||
|
||||
COLLECTION
|
||||
: (`any`) Typically a page collection or a [slice] of [maps].
|
||||
: (`any`) A [page collection] or a [slice] of [maps].
|
||||
|
||||
[maps]: /getting-started/glossary/#map
|
||||
[page collection]: /getting-started/glossary/#page-collection
|
||||
[slice]: /getting-started/glossary/#slice
|
||||
|
||||
KEY
|
||||
@@ -50,7 +46,7 @@ KEY
|
||||
```
|
||||
|
||||
[chain]: /getting-started/glossary/#chain
|
||||
|
||||
Typically a
|
||||
OPERATOR
|
||||
: (`string`) The logical comparison [operator](#operators).
|
||||
|
||||
@@ -64,7 +60,7 @@ Comparison|Result
|
||||
`false "eq" "false"`|`false`
|
||||
`false "eq" false`|`true`
|
||||
|
||||
When one or both of the values to compare is a slice, use the `in`, `not in` or `intersect` operators as described below.
|
||||
When one or both of the values to compare is a slice, use the `in`, `not in`, or `intersect` operators as described below.
|
||||
|
||||
## Operators
|
||||
|
||||
@@ -123,14 +119,14 @@ Compare the value of the given field to an [`int`] or [`float`]:
|
||||
[`float`]: /getting-started/glossary/#float
|
||||
|
||||
```go-html-template
|
||||
{{ $sectionPages := where site.RegularPages "Section" "eq" "books" }}
|
||||
{{ $books := where site.RegularPages "Section" "eq" "books" }}
|
||||
|
||||
{{ $pages := where $sectionPages "Params.price" "eq" 42 }}
|
||||
{{ $pages := where $sectionPages "Params.price" "ne" 42.67 }}
|
||||
{{ $pages := where $sectionPages "Params.price" "ge" 42 }}
|
||||
{{ $pages := where $sectionPages "Params.price" "gt" 42.67 }}
|
||||
{{ $pages := where $sectionPages "Params.price" "le" 42 }}
|
||||
{{ $pages := where $sectionPages "Params.price" "lt" 42.67 }}
|
||||
{{ $pages := where $books "Params.price" "eq" 42 }}
|
||||
{{ $pages := where $books "Params.price" "ne" 42.67 }}
|
||||
{{ $pages := where $books "Params.price" "ge" 42 }}
|
||||
{{ $pages := where $books "Params.price" "gt" 42.67 }}
|
||||
{{ $pages := where $books "Params.price" "le" 42 }}
|
||||
{{ $pages := where $books "Params.price" "lt" 42.67 }}
|
||||
```
|
||||
|
||||
## Boolean comparison
|
||||
@@ -140,12 +136,12 @@ Compare the value of the given field to a [`bool`]:
|
||||
[`bool`]: /getting-started/glossary/#bool
|
||||
|
||||
```go-html-template
|
||||
{{ $sectionPages := where site.RegularPages "Section" "eq" "books" }}
|
||||
{{ $books := where site.RegularPages "Section" "eq" "books" }}
|
||||
|
||||
{{ $pages := where $sectionPages "Params.fiction" "eq" true }}
|
||||
{{ $pages := where $sectionPages "Params.fiction" "eq" false }}
|
||||
{{ $pages := where $sectionPages "Params.fiction" "ne" true }}
|
||||
{{ $pages := where $sectionPages "Params.fiction" "ne" false }}
|
||||
{{ $pages := where $books "Params.fiction" "eq" true }}
|
||||
{{ $pages := where $books "Params.fiction" "eq" false }}
|
||||
{{ $pages := where $books "Params.fiction" "ne" true }}
|
||||
{{ $pages := where $books "Params.fiction" "ne" false }}
|
||||
```
|
||||
|
||||
## Member comparison
|
||||
@@ -158,19 +154,19 @@ Compare a [`scalar`] to a [`slice`].
|
||||
For example, to return a collection of pages where the `color` page parameter is either "red" or "yellow":
|
||||
|
||||
```go-html-template
|
||||
{{ $sectionPages := where site.RegularPages "Section" "eq" "fruit" }}
|
||||
{{ $fruit := where site.RegularPages "Section" "eq" "fruit" }}
|
||||
|
||||
{{ $colors := slice "red" "yellow" }}
|
||||
{{ $pages := where $sectionPages "Params.color" "in" $colors }}
|
||||
{{ $pages := where $fruit "Params.color" "in" $colors }}
|
||||
```
|
||||
|
||||
To return a collection of pages where the "color" page parameter is neither "red" nor "yellow":
|
||||
|
||||
```go-html-template
|
||||
{{ $sectionPages := where site.RegularPages "Section" "eq" "fruit" }}
|
||||
{{ $fruit := where site.RegularPages "Section" "eq" "fruit" }}
|
||||
|
||||
{{ $colors := slice "red" "yellow" }}
|
||||
{{ $pages := where $sectionPages "Params.color" "not in" $colors }}
|
||||
{{ $pages := where $fruit "Params.color" "not in" $colors }}
|
||||
```
|
||||
|
||||
## Intersection comparison
|
||||
@@ -180,10 +176,10 @@ Compare a [`slice`] to a [`slice`], returning collection elements with common va
|
||||
For example, to return a collection of pages where any of the terms in the "genres" taxonomy are "suspense" or "romance":
|
||||
|
||||
```go-html-template
|
||||
{{ $sectionPages := where site.RegularPages "Section" "eq" "books" }}
|
||||
{{ $books := where site.RegularPages "Section" "eq" "books" }}
|
||||
|
||||
{{ $genres := slice "suspense" "romance" }}
|
||||
{{ $pages := where $sectionPages "Params.genres" "intersect" $genres }}
|
||||
{{ $pages := where $books "Params.genres" "intersect" $genres }}
|
||||
```
|
||||
|
||||
## Regular expression comparison
|
||||
|
Reference in New Issue
Block a user