hugo/content/functions/where.md
Bjørn Erik Pedersen 047c4188df Squashed 'docs/' changes from d9c8fc220..f9a5dc59b
f9a5dc59b Code Toggle block added to doc site final batch Templates  Variables 
4d4107968 Add eSolia as new sponsor
000fed94e Add missing closing tags for li in menu template example
f462b620f Clarify that local CSV files cannot be inside data dir
ae083641a Added hugo-search-index to list of search tools
e2b64d0b7 Remove extra link
2fb4c9af5 Release 0.38.2
59b1c9853 releaser: Prepare repository for 0.39-DEV
92f6a05ea releaser: Add release notes to /docs for release of 0.38.2
76244729e releaser: Bump versions for release of 0.38.2
0960c5fb3 Adjust gray color of tab vs pane in code-toggle.
8ae3aadd7 use code-toggle shortcode when relevant Content Management 
455b8b53b Update related.md
6e8d19090 Release 0.38.1
079ba044c releaser: Prepare repository for 0.39-DEV
6f23e6ec1 releaser: Add release notes to /docs for release of 0.38.1
c51692ceb releaser: Bump versions for release of 0.38.1
d37ea6a5e Update related.md
faa2707d0 Update index.md
9ce901dcb Add a forgotten language tag (go-html-template) for code
b05aaed14 Update where.md
4d4760819 Fix typo in code-toggle.md
c5a5250a1 Use the new go-html-template Chroma lexer
2de831f4b Add the full list of Chroma lexers
18114d4b4 Update Output Formats
b069d7f84 Release 0.38
caaa8355a releaser: Prepare repository for 0.39-DEV
e45b7cc9f releaser: Add release notes to /docs for release of 0.38
40f40906e releaser: Bump versions for release of 0.38
2d52e2e4e Merge commit 'ed8bf081fdbf336e026517b7e1b123c039014ab5'
1439f64a0 docs: Generate docshelper data
5b0edfd79 Add .Site.IsServer
fdb579ad1 Merge commit '0a23baa6a90901f772c234107c4f12c16c76f4aa'
7b71da1f8 hugolib: Add Reset method to delete key from Scratch
63a131664 docs: Add docs for lang.Merge
55cba056d Merge commit '3886fc1fef6ac19d58b9ba1bb642d0c6c9a54031'
6f301ebcc docs: Add docs on the new front matter configuration
7ba35ef56 Merge commit 'c0290655825e7bb36e13fb39f89d85b392cf1adc'
3d2cab754 releaser: Prepare repository for 0.38-DEV
095e888e1 releaser: Add release notes to /docs for release of 0.37.1
593fa0dcb releaser: Bump versions for release of 0.37.1
c18c1df54 releaser: Prepare repository for 0.38-DEV

git-subtree-dir: docs
git-subtree-split: f9a5dc59b77d15cc2c7534e10bcd90bcfeda7bb4
2018-04-16 07:43:59 +02:00

154 lines
4.4 KiB
Markdown

---
title: where
# linktitle: where
description: Filters an array to only the elements containing a matching value for a given field.
godocref:
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-02-01
categories: [functions]
menu:
docs:
parent: "functions"
keywords: [filtering]
signature: ["where COLLECTION KEY [OPERATOR] MATCH"]
workson: [lists,taxonomies,terms,groups]
hugoversion:
relatedfuncs: [intersect,first,after,last]
deprecated: false
toc: true
needsexample: true
---
`where` filters an array to only the elements containing a matching value for a given field.
```go-html-template
{{ range where .Data.Pages "Section" "post" }}
{{ .Content }}
{{ end }}
```
It can be used by dot-chaining the second argument to refer to a nested element of a value.
```
+++
series: golang
+++
```
```go-html-template
{{ range where .Site.Pages "Params.series" "golang" }}
{{ .Content }}
{{ end }}
```
It can also be used with the logical operators `!=`, `>=`, `in`, etc. Without an operator, `where` compares a given field with a matching value equivalent to `=`.
```go-html-template
{{ range where .Data.Pages "Section" "!=" "post" }}
{{ .Content }}
{{ end }}
```
The following logical operators are available with `where`:
`=`, `==`, `eq`
: `true` if a given field value equals a matching value
`!=`, `<>`, `ne`
: `true` if a given field value doesn't equal a matching value
`>=`, `ge`
: `true` if a given field value is greater than or equal to a matching value
`>`, `gt`
: `true` if a given field value is greater than a matching value
`<=`, `le`
: `true` if a given field value is lesser than or equal to a matching value
`<`, `lt`
: `true` if a given field value is lesser than a matching value
`in`
: `true` if a given field value is included in a matching value; a matching value must be an array or a slice
`not in`
: `true` if a given field value isn't included in a matching value; a matching value must be an array or a slice
`intersect`
: `true` if a given field value that is a slice/array of strings or integers contains elements in common with the matching value; it follows the same rules as the [`intersect` function][intersect].
## Use `where` with `intersect`
```go-html-template
{{ range where .Site.Pages ".Params.tags" "intersect" .Params.tags }}
{{ if ne .Permalink $.Permalink }}
{{ .Render "summary" }}
{{ end }}
{{ end }}
```
You can also put the returned value of the `where` clauses into a variable:
{{< code file="where-intersect-variables.html" >}}
{{ $v1 := where .Site.Pages "Params.a" "v1" }}
{{ $v2 := where .Site.Pages "Params.b" "v2" }}
{{ $filtered := $v1 | intersect $v2 }}
{{ range $filtered }}
{{ end }}
{{< /code >}}
## Use `where` with `first`
The following grabs the first five content files in `post` using the [default ordering](/templates/lists/) for lists (i.e., `weight => date`):
{{< code file="where-with-first.html" >}}
{{ range first 5 (where .Data.Pages "Section" "post") }}
{{ .Content }}
{{ end }}
{{< /code >}}
## Nest `where` Clauses
You can also nest `where` clauses to drill down on lists of content by more than one parameter. The following first grabs all pages in the "blog" section and then ranges through the result of the first `where` clause and finds all pages that are *not* featured:
```go-html-template
{{ range where (where .Data.Pages "Section" "blog" ) ".Params.featured" "!=" "true" }}
```
## Unset Fields
Filtering only works for set fields. To check whether a field is set or exists, you can use the operand `nil`.
This can be useful to filter a small amount of pages from a large pool. Instead of set field on all pages, you can set field on required pages only.
Only the following operators are available for `nil`
* `=`, `==`, `eq`: True if the given field is not set.
* `!=`, `<>`, `ne`: True if the given field is set.
```go-html-template
{{ range where .Data.Pages ".Params.specialpost" "!=" nil }}
{{ .Content }}
{{ end }}
```
## Portable `where` filters
This is especially important for themes, but to list the most relevant pages on the front page or similar, you can use `.Site.Params.mainSections` list.
This will, by default, list pages from the _section with the most pages_.
```go-html-template
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
```
The user can override the default in `config.toml`:
```toml
[params]
mainSections = ["blog", "docs"]
```
[intersect]: /functions/intersect/