mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-30 01:03:21 -05:00
Adding documentation about 'where' and cleaning up docs around first.
This commit is contained in:
parent
0ce4ec1edb
commit
93addfcbee
3 changed files with 63 additions and 2 deletions
|
@ -2,7 +2,7 @@
|
||||||
aliases:
|
aliases:
|
||||||
- /layout/functions/
|
- /layout/functions/
|
||||||
date: 2013-07-01
|
date: 2013-07-01
|
||||||
linktitle: Single
|
linktitle: Single Content
|
||||||
menu:
|
menu:
|
||||||
main:
|
main:
|
||||||
parent: layout
|
parent: layout
|
||||||
|
|
|
@ -38,11 +38,32 @@ eg. {{echoParam .Params "project_url" }}
|
||||||
### first
|
### first
|
||||||
Slices an array to only the first X elements.
|
Slices an array to only the first X elements.
|
||||||
|
|
||||||
|
Works on [lists](/templates/list/), [taxonomies](/taxonomies/displaying/), [terms](/templates/terms/), [groups](/templates/list/)
|
||||||
|
|
||||||
eg.
|
eg.
|
||||||
{{ range first 10 .Data.Pages }}
|
{{ range first 10 .Data.Pages }}
|
||||||
{{ .Render "summary"}}
|
{{ .Render "summary"}}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
### where
|
||||||
|
Filters an array to only elements containing a matching value for a given field.
|
||||||
|
|
||||||
|
Works on [lists](/templates/list/), [taxonomies](/taxonomies/displaying/), [terms](/templates/terms/), [groups](/templates/list/)
|
||||||
|
|
||||||
|
eg.
|
||||||
|
|
||||||
|
{{ range where .Data.Pages "Section" "post" }}
|
||||||
|
{{ .Content}}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
*where and first can be stacked*
|
||||||
|
|
||||||
|
eg.
|
||||||
|
|
||||||
|
{{ range first 5 (where .Data.Pages "Section" "post") }}
|
||||||
|
{{ .Content}}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
|
||||||
## Math
|
## Math
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
aliases:
|
aliases:
|
||||||
- /layout/indexes/
|
- /layout/indexes/
|
||||||
date: 2013-07-01
|
date: 2013-07-01
|
||||||
linktitle: List
|
linktitle: List of Content
|
||||||
menu:
|
menu:
|
||||||
main:
|
main:
|
||||||
parent: layout
|
parent: layout
|
||||||
|
@ -296,3 +296,43 @@ within each group in alphabetical order by title.
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</ul>
|
</ul>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
|
## Filtering & Limiting Content
|
||||||
|
|
||||||
|
Sometimes you only want to list a subset of the available content. A common
|
||||||
|
request is to only display “Posts” on the homepage. Using the `where` function
|
||||||
|
you can do just that.
|
||||||
|
|
||||||
|
### First
|
||||||
|
|
||||||
|
`first` works like the limit keyword in SQL. It reduces the array to only the
|
||||||
|
first X elements. It takes the array and number of elements as input.
|
||||||
|
|
||||||
|
{{ range first 10 .Data.Pages }}
|
||||||
|
{{ .Render "summary"}}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
### Where
|
||||||
|
|
||||||
|
`where` works in a similar manner to the where keyword in SQL. It selects all
|
||||||
|
elements of the slice that match the provided field and value. It takes three
|
||||||
|
arguments 'array or slice of maps or structs', 'key or field name' and 'match
|
||||||
|
value'
|
||||||
|
|
||||||
|
{{ range where .Data.Pages "Section" "post" }}
|
||||||
|
{{ .Content}}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
### First & Where Together
|
||||||
|
|
||||||
|
Using both together can be very powerful.
|
||||||
|
|
||||||
|
{{ range first 5 (where .Data.Pages "Section" "post") }}
|
||||||
|
{{ .Content}}
|
||||||
|
{{ end }}
|
||||||
|
|
||||||
|
If `where` or `first` receives invalid input or a field name that doesn’t exist they will provide an error and stop site generation.
|
||||||
|
|
||||||
|
These are both template functions and work on not only
|
||||||
|
[lists](/templates/list/), but [taxonomies](/taxonomies/displaying/),
|
||||||
|
[terms](/templates/terms/) and [groups](/templates/list/).
|
||||||
|
|
Loading…
Reference in a new issue