mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
07b8d9466d
fb551cc75 Update index.md 7af894857 Update index.md d235753ea Hugo 0.82.1 e03e72deb Merge branch 'temp0821' e62648961 Merge branch 'release-0.82.1' e1ab0f6eb releaser: Add release notes to /docs for release of 0.82.1 5d354c38d Replaced ``` code blocks with Code Toggler c9d065c20 Remove duplicate YAML keys (#1420) 8ae31e701 Add webp image encoding support 848f2af26 Update internal.md (#1407) c103a86a4 Fix `ref` shortcode example output (#1409) 9f8ba56dc Remove leading dot from where function KEY (#1419) 363251a51 Improve presentation of template lookup order (#1382) b73da986d Improve description of Page Resources (#1381) 4e0bb96d5 Rework robots.txt page (#1405) edf893e6f Update migrations.md (#1412) 450f1580b Add link to `site` function doc (#1417) cfffa6e6f Added one extension to the list (#1414) 05f1665a0 Update theme 5de0b1c6a Update theme 250e20552 Add hugo.IsExtended dea5e1fd7 Fix typo on merge function page (#1408) 1bbed2cf3 Update configuration.md be0b64a46 Omit ISO cbb5b8367 Fix `dateFormat` documentation 698f15466 Regenerate the docshelper f9a8a7cb6 Update multilingual.md a22dc6267 Fix grammar (#1398) eb98b0997 Fix pretty URL example (#1397) f4c4153dc Mention date var complementation in post scheduling (#1396) 17fae284c Fix resources.ExecuteAsTemplate argument order (#1394) 97e2c2abb Use code-toggle shortcode in `multilingual.md` (#1388) 3a84929bb Harmonize capitalization (#1393) 17f15daa6 fix file naming used in example (#1392) 5d97b6a18 Add slice syntax to sections permalinks config 00665b97b Improve description of `site.md` edcf5e3fc Fix example in `merge.md` f275ab778 Update postprocess.md 9593e3991 Fix file name 59bd9656f Update postprocess.md 1172fb6d0 Update to theNewDynamic repository (#1263) f5b5c1d2c Update Hugo container image 4f2e92f2a Adapt anchorize.md to Goldmark 98aa19073 Directly link to `highlight` shortcode (#1384) 4c75c2422 Fix header level f15c06f23 markdownify: add note about render-hooks and .RenderString (#1281) 69c82eb68 Remove Blackfriday reference from shortcode desc (#1380) 36de478df Update description of ignoreFiles config setting (#1377) 6337699d8 Remove "Authors" page from documentation (#1371) 35e73ca90 fix indent in example (#1372) d3f01f19a Remove opening body tag from header example (#1376) 341a5a7d8 Update index.md c9bfdbee6 Release 0.82.0 119644949 releaser: Add release notes to /docs for release of 0.82.0 32efaed78 docs: Regenerate docs helper dea5449a2 docs: Regen CLI docs eeab18fce Merge commit '81689af79901f0cdaff765cda6322dd4a9a7ccb3' d508a1259 Attributes for code fences should be placed after the lang indicator only c80905cef deps: Update to esbuild v0.9.0 95350eb79 Add support for Google Analytics v4 02d36f9bc Allow markdown attribute lists to be used in title render hooks 7df220a64 Merge commit '9d31f650da964a52f05fc27b7fb99cf3e09778cf' d80bf61b7 Fixes #7698. git-subtree-dir: docs git-subtree-split: fb551cc750faa83a1493b0e0d0898cd98ab74465
177 lines
5.1 KiB
Markdown
177 lines
5.1 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.
|
|
|
|
It works in a similar manner to the [`where` keyword in
|
|
SQL][wherekeyword].
|
|
|
|
```go-html-template
|
|
{{ range where .Pages "Section" "foo" }}
|
|
{{ .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 .Pages "Section" "!=" "foo" }}
|
|
{{ .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 `Booleans`
|
|
When using booleans you should not put quotation marks.
|
|
```go-html-template
|
|
{{range where .Pages "Draft" true}}
|
|
<p>{{.Title}}</p>
|
|
{{end}}
|
|
```
|
|
|
|
|
|
## 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`
|
|
|
|
Using `first` and `where` together can be very
|
|
powerful. Below snippet gets a list of posts only from [**main
|
|
sections**](#mainsections), sorts it using the [default
|
|
ordering](/templates/lists/) for lists (i.e., `weight => date`), and
|
|
then ranges through only the first 5 posts in that list:
|
|
|
|
{{< code file="first-and-where-together.html" >}}
|
|
{{ range first 5 (where site.RegularPages "Type" "in" site.Params.mainSections) }}
|
|
{{ .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 .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 setting a field on all pages, you can set that 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 .Pages "Params.specialpost" "!=" nil }}
|
|
{{ .Content }}
|
|
{{ end }}
|
|
```
|
|
|
|
## Portable `where` filters -- `site.Params.mainSections` {#mainsections}
|
|
|
|
**This is especially important for themes.**
|
|
|
|
To list the most relevant pages on the front page or similar, you
|
|
should use the `site.Params.mainSections` list instead of comparing
|
|
section names to hard-coded values like `"posts"` or `"post"`.
|
|
|
|
```go-html-template
|
|
{{ $pages := where site.RegularPages "Type" "in" site.Params.mainSections }}
|
|
```
|
|
|
|
If the user has not set this config parameter in their site config, it
|
|
will default to the _section with the most pages_.
|
|
|
|
The user can override the default:
|
|
|
|
{{< code-toggle file="config" >}}
|
|
[params]
|
|
mainSections = ["blog", "docs"]
|
|
{{< /code-toggle >}}
|
|
|
|
[intersect]: /functions/intersect/
|
|
[wherekeyword]: https://www.techonthenet.com/sql/where.php
|