hugo/content/templates/lists.md

600 lines
19 KiB
Markdown
Raw Normal View History

---
title: Lists of Content in Hugo
linktitle: List Page Templates
description: Lists have a specific meaning and usage in Hugo when it comes to rendering your site homepage, section page, taxonomy list, or taxonomy terms list.
date: 2017-02-01
publishdate: 2017-02-01
lastmod: 2017-02-01
categories: [templates]
keywords: [lists,sections,rss,taxonomies,terms]
menu:
docs:
parent: "templates"
weight: 22
weight: 22
sections_weight: 22
draft: false
aliases: [/templates/list/,/layout/indexes/]
toc: true
---
## What is a List Page Template?
{{< youtube 8b2YTSMdMps >}}
A list page template is a template used to render multiple pieces of content in a single HTML page. The exception to this rule is the homepage, which is still a list but has its own [dedicated template][homepage].
Hugo uses the term *list* in its truest sense; i.e. a sequential arrangement of material, especially in alphabetical or numerical order. Hugo uses list templates on any output HTML page where content is traditionally listed:
* [Taxonomy terms pages][taxterms]
* [Taxonomy list pages][taxlists]
* [Section list pages][sectiontemps]
* [RSS][rss]
Squashed 'docs/' changes from 56c34962c..dce236ad1 dce236ad1 Wrap up the bundle etc. edits for now 27d058566 Add the bundle tree to the organization bundle a83f750dd Move organization.md to its own bundle 01ec4f462 Make the image docs a bundle 60de1e090 Some more resources copy-edits 05d763c0c Some resource copy-edits 6299d6dbb Update the imgproc shortcode 59e0fc209 Add headless bundle documentation a3bbf60bf Link Page Resources page from Front Matter page 830576f86 Update order significance section, add counter section 3b1836509 Revert the recent change made to default list template 886ed0e10 Page Bundles draft rev 2 f530d1a7a image processing and page resources made into regular .md ec47cecda Finalised Page Resources and Image Processing Moved Page Resources and Impage Processing out of the Bundle section and at the root of the Content Management section 253092335 Modified front matter metadata exemple. Added yaml version. da5e4f476 Adding date in the front-matter; missed in previous commit 6bc3ced13 Add rough draft for page and section bundles a0e44458f Image processing first draft, Resources second read/fix 2367f0b78 data: Remove duplicate layouts in table c2f179839 First draft of bundles/resources (covers resources and metadata) 2a3f9a613 Add weights to pages in Bundles branch 9a0146cc0 Switch front-matter format of Bundles doc to yaml; add front-matter 1295fc083 First draft for Bundles documentation organization structure 5a2e52231 Fix archetype paths 9c2e5c063 Merge commit '22cced34fc608256f8271ad591a5ccca991bb164' 22cced34f Squashed 'themes/gohugoioTheme/' changes from 75da2f6b..ecad8247 55d16c9a1 Fix broken sentence in multilingual sections a76895ad2 Replace the outdated Emacs package with new one e6cf1dec0 Remove obsolete link to hugo roadmap dd2fd145b Add GitLab Pages to mentioned hosters (#309) a05ce6bf6 Add 0.34 release notes poster 5c0ebdfca Release 0.34 13c2f3dc8 Merge branch 'temp34' e6b5ffa04 Add 0.34 poster 1e1960496 releaser: Add release notes to /docs for release of 0.34 ac3efe182 releaser: Bump versions for release of 0.34 8f91f62d8 Fixes #222 cca35dbe4 Fix example eaaa21ca1 Add missing params key 00d0b0363 Adding new Blogger utility to tools/migrations 7d36d579e Updated the line number for Dockerfile pointer 852188f85 Update installing.md with Fedora instructions 4d151a3ab Update search.md 4c2750bfb Update deployment-with-nanobox.md c3cc9cd49 configuration: Remove defaultExtension from docs f7c96b4b5 Update GitHub Pages documentation 55787f09a Merge branch 'rmetzler-menu-link-title' 2abbd9bd9 Merge branch 'master' into menu-link-title e1fd710b7 Bring archetypes in from theme. daf6f51c0 Mention the significance of leading 0 in int fn string input 07f498755 Add documentation for `cond` function. 050ccd12b Add documentation for the .HasShortcode function 919af9071 Correct anchor under 'Add custom metadata to a Taxonomy Term' 55600b4ff More layouts work 201cf4f67 Add some more single page layout variants d5e7c03e2 Rework the layouts doc 84622e67c Cleans up the code sample c231c9bd5 Add a new note to 0.33 relnotes 328ec9930 Release 0.33 b108fcc7b Merge branch 'temp33' into next ab9d9ee65 releaser: Prepare repository for 0.34-DEV e20c75320 releaser: Add release notes to /docs for release of 0.33 49f24dcd1 releaser: Bump versions for release of 0.33 9c8e5e207 Update 0.33 poster 7655603c8 Regenerate the docshelper data 16dc99583 Add Hugo 0.33 poster ce40cc197 Merge commit '3cf4300097610bb8b5bd0686d96d1df5db641895' 9a3085523 releaser: Prepare repository for 0.33-DEV a52db97d8 fixing typos and syntax for consistency 64525670f ádd title to some menu entries. This needs hugo >= v0.32 85d415ab2 ádd examples for menu .Title and .Page git-subtree-dir: docs git-subtree-split: dce236ad1258a9d9a0ee209f02b2e1f65b46f0fb
2018-01-31 05:07:47 -05:00
For template lookup order, see [Template Lookup](/templates/lookup-order/).
The idea of a list page comes from the [hierarchical mental model of the web][mentalmodel] and is best demonstrated visually:
![Image demonstrating a hierarchical website sitemap.](/images/site-hierarchy.svg)
## List Defaults
### Default Templates
Since section lists and taxonomy lists (N.B., *not* [taxonomy terms lists][taxterms]) are both *lists* with regards to their templates, both have the same terminating default of `_default/list.html` or `themes/<THEME>/layouts/_default/list.html` in their lookup order. In addition, both [section lists][sectiontemps] and [taxonomy lists][taxlists] have their own default list templates in `_default`:
#### Default Section Templates
1. `layouts/_default/section.html`
2. `layouts/_default/list.html`
#### Default Taxonomy List Templates
1. `layouts/_default/taxonomy.html`
2. `themes/<THEME>/layouts/_default/taxonomy.html`
## Add Content and Front Matter to List Pages
Since v0.18, [everything in Hugo is a `Page`][bepsays]. This means list pages and the homepage can have associated content files (i.e. `_index.md`) that contain page metadata (i.e., front matter) and content.
This new model allows you to include list-specific front matter via `.Params` and also means that list templates (e.g., `layouts/_default/list.html`) have access to all [page variables][pagevars].
{{% note %}}
It is important to note that all `_index.md` content files will render according to a *list* template and not according to a [single page template](/templates/single-page-templates/).
{{% /note %}}
### Example Project Directory
The following is an example of a typical Hugo project directory's content:
```
.
...
├── content
| ├── post
| | ├── _index.md
| | ├── post-01.md
| | └── post-02.md
| └── quote
| | ├── quote-01.md
| | └── quote-02.md
...
```
Using the above example, let's assume you have the following in `content/post/_index.md`:
{{< code file="content/post/_index.md" >}}
---
title: My Golang Journey
date: 2017-03-23
publishdate: 2017-03-24
---
I decided to start learning Golang in March 2017.
Follow my journey through this new blog.
{{< /code >}}
You can now access this `_index.md`'s' content in your list template:
{{< code file="layouts/_default/list.html" download="list.html" >}}
{{ define "main" }}
<main>
<article>
<header>
<h1>{{.Title}}</h1>
</header>
<!-- "{{.Content}}" pulls from the markdown content of the corresponding _index.md -->
{{.Content}}
</article>
<ul>
<!-- Ranges through content/post/*.md -->
{{ range .Data.Pages }}
<li>
<a href="{{.Permalink}}">{{.Date.Format "2006-01-02"}} | {{.Title}}</a>
</li>
{{ end }}
</ul>
</main>
{{ end }}
{{< /code >}}
This above will output the following HTML:
{{< code file="example.com/post/index.html" copy="false" >}}
<!--top of your baseof code-->
<main>
<article>
<header>
<h1>My Golang Journey</h1>
</header>
<p>I decided to start learning Golang in March 2017.</p>
<p>Follow my journey through this new blog.</p>
</article>
<ul>
<li><a href="/post/post-01/">Post 1</a></li>
<li><a href="/post/post-02/">Post 2</a></li>
</ul>
</main>
<!--bottom of your baseof-->
{{< /code >}}
### List Pages Without `_index.md`
You do *not* have to create an `_index.md` file for every list page (i.e. section, taxonomy, taxonomy terms, etc) or the homepage. If Hugo does not find an `_index.md` within the respective content section when rendering a list template, the page will be created but with no `{{.Content}}` and only the default values for `.Title` etc.
Squashed 'docs/' changes from 2fc775dec..f97826a17 f97826a17 Merge commit '12ecbf4a1b05c2794281f47909c836b1a005bc19' 12ecbf4a1 Squashed 'themes/gohugoioTheme/' changes from ecad8247..fe71e360 aaa7ac214 Ignore .DS_Store files 0e023ca12 Remove comments in showcase front matter f3ba5ac87 Hartwell Insurance showcase 47aefdbda Remove unnecessary duplicated words 612693b4f Some minor language fixes a833ba15b Spelling 5972e70a9 Revert "showcase: Even smaller file" 3479b975e showcase: Even smaller file 2272e0b83 showcase: Reduce image size 718c3c3e5 Spelling 0b954eccb showcase: Pace copy-edits baded8064 showcase: Pace 842c1cedf Add a Showcase copyright notice c5963edaa Update installing 4fa0b0d73 Add link to showcase template d0f303916 Add some showcase guide content c809789b1 Add srcset to the showcase screenshots 49d875904 Start of a template 1153de221 Clean resouerces 69f00df4c Remove the showcases 653ad5bcd Add a simple byline c5342b5e5 Regenerate image after rebase b80669b34 Bump version in config.toml cd5c79c67 Fix an issue where whitespace wrap caused scrollbars on some browsers f00547e98 Add section to the title element aa10defed Iterating on Showcase (#330) 76a0bfbc3 Add some dummy content to some of the showcase pages 66f68964d Standardize showcase metadata 627ffa6d4 Adjust showcase image size on front page cbc3efece Redo some showcase images 054b54cb6 Use .RelPermalink and not .URL 82ba5f1c6 Add resources d1415795b Move showcase items in list view to a template in the layout f34620e90 Tweak details box for mobile df6a0bf24 Include images that changed from the column shift 02036581f Improve showcase single layout 5f7730c89 Improve styling of showcase prev/next buttons a2b2f7731 Remove extra div that was breaking mobile layout of the showcase b172fe5f4 Add block class to to images on mobile so they behave as expected a4ebfec86 Add a proper RSS feed to home page 0524479e0 Move showcase images to proper Resources 0544b57df Convert showcase pages to bundles 8febaab2b Add Showcase to Home page and internal pages 26d1f4542 Fix baseURL in Netlify deploy previews 046497616 Revert "Try to fix the Netlify preview baseURL setup" 80dce17ca Revert "Add some temporary baseURL debug" 8617e8692 Add some temporary baseURL debug 371e56bce Try to fix the Netlify preview baseURL setup 1b70b3f18 Add Netlify CMS to Frontends list d6184e71d Fix menu for "What is Hugo" page 1ae83ad3e Fix mobile menu display so it shows on mid-size displays f60e1f750 Edited slight typo, added "of" 56b906667 Fix typo c5bea5cbd Release 0.36 00539094e releaser: Prepare repository for 0.37-DEV b222cbdf2 releaser: Add release notes to /docs for release of 0.36 e59d1d766 releaser: Bump versions for release of 0.36 9620aa002 docs: Add documentation for smart cropping etc. 91c3801f1 Merge commit 'c305e44f5f081e4436195923a4593e396f07cd49' 8e71ff60b releaser: Prepare repository for 0.36-DEV git-subtree-dir: docs git-subtree-split: f97826a17209fe3e153b7f5bbf69c511e4e13203
2018-02-10 06:51:31 -05:00
Using this same `layouts/_default/list.html` template and applying it to the `quotes` section above will render the following output. Note that `quotes` does not have an `_index.md` file to pull from:
{{< code file="example.com/quote/index.html" copy="false" >}}
<!--baseof-->
<main>
<article>
<header>
<!-- Hugo assumes that .Title is the name of the section since there is no _index.md content file from which to pull a "title:" field -->
<h1>Quotes</h1>
</header>
</article>
<ul>
<li><a href="https://example.com/quote/quotes-01/">Quote 1</a></li>
<li><a href="https://example.com/quote/quotes-02/">Quote 2</a></li>
</ul>
</main>
<!--baseof-->
{{< /code >}}
{{% note %}}
The default behavior of Hugo is to pluralize list titles; hence the inflection of the `quote` section to "Quotes" when called with the `.Title` [page variable](/variables/page/). You can change this via the `pluralizeListTitles` directive in your [site configuration](/getting-started/configuration/).
{{% /note %}}
## Example List Templates
### Section Template
This list template has been modified slightly from a template originally used in [spf13.com](http://spf13.com/). It makes use of [partial templates][partials] for the chrome of the rendered page rather than using a [base template][base] The examples that follow also use the [content view templates][views] `li.html` or `summary.html`.
{{< code file="layouts/section/post.html" >}}
{{ partial "header.html" . }}
{{ partial "subheader.html" . }}
<main>
<div>
<h1>{{ .Title }}</h1>
<ul>
<!-- Renders the li.html content view for each content/post/*.md -->
{{ range .Data.Pages }}
{{ .Render "li"}}
{{ end }}
</ul>
</div>
</main>
{{ partial "footer.html" . }}
{{< /code >}}
### Taxonomy Template
{{< code file="layouts/_default/taxonomies.html" download="taxonomies.html" >}}
{{ define "main" }}
<main>
<div>
<h1>{{ .Title }}</h1>
<!-- ranges through each of the content files associated with a particular taxonomy term and renders the summary.html content view -->
{{ range .Data.Pages }}
{{ .Render "summary"}}
{{ end }}
</div>
</main>
{{ end }}
{{< /code >}}
## Order Content
Hugo lists render the content based on metadata you provide in [front matter][]. In addition to sane defaults, Hugo also ships with multiple methods to make quick work of ordering content inside list templates:
### Default: Weight > Date > LinkTitle > FilePath
{{< code file="layouts/partials/default-order.html" >}}
<ul>
{{ range .Data.Pages }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Weight
{{< code file="layouts/partials/by-weight.html" >}}
<ul>
{{ range .Data.Pages.ByWeight }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Date
{{< code file="layouts/partials/by-date.html" >}}
<ul>
<!-- orders content according to the "date" field in front matter -->
{{ range .Data.Pages.ByDate }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Publish Date
{{< code file="layouts/partials/by-publish-date.html" >}}
<ul>
<!-- orders content according to the "publishdate" field in front matter -->
{{ range .Data.Pages.ByPublishDate }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Expiration Date
{{< code file="layouts/partials/by-expiry-date.html" >}}
<ul>
{{ range .Data.Pages.ByExpiryDate }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Last Modified Date
{{< code file="layouts/partials/by-last-mod.html" >}}
<ul>
<!-- orders content according to the "lastmod" field in front matter -->
{{ range .Data.Pages.ByLastmod }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Length
{{< code file="layouts/partials/by-length.html" >}}
<ul>
<!-- orders content according to content length in ascending order (i.e., the shortest content will be listed first) -->
{{ range .Data.Pages.ByLength }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Title
{{< code file="layouts/partials/by-title.html" >}}
<ul>
<!-- ranges through content in ascending order according to the "title" field set in front matter -->
{{ range .Data.Pages.ByTitle }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Link Title
{{< code file="layouts/partials/by-link-title.html" >}}
<ul>
<!-- ranges through content in ascending order according to the "linktitle" field in front matter. If a "linktitle" field is not set, the range will start with content that only has a "title" field and use that value for .LinkTitle -->
{{ range .Data.Pages.ByLinkTitle }}
<li>
<h1><a href="{{ .Permalink }}">{{ .LinkTitle }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
### By Parameter
Order based on the specified front matter parameter. Content that does not have the specified front matter field will use the site's `.Site.Params` default. If the parameter is not found at all in some entries, those entries will appear together at the end of the ordering.
{{< code file="layouts/partials/by-rating.html" >}}
<!-- Ranges through content according to the "rating" field set in front matter -->
{{ range (.Data.Pages.ByParam "rating") }}
<!-- ... -->
{{ end }}
{{< /code >}}
If the targeted front matter field is nested beneath another field, you can access the field using dot notation.
{{< code file="layouts/partials/by-nested-param.html" >}}
{{ range (.Data.Pages.ByParam "author.last_name") }}
<!-- ... -->
{{ end }}
{{< /code >}}
### Reverse Order
Reversing order can be applied to any of the above methods. The following uses `ByDate` as an example:
{{< code file="layouts/partials/by-date-reverse.html" >}}
<ul>
{{ range .Data.Pages.ByDate.Reverse }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{< /code >}}
## Group Content
Hugo provides some functions for grouping pages by Section, Type, Date, etc.
### By Page Field
{{< code file="layouts/partials/by-page-field.html" >}}
<!-- Groups content according to content section. The ".Key" in this instance will be the section's title. -->
{{ range .Data.Pages.GroupBy "Section" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{< /code >}}
In the above example, you may want `{{.Title}}` to point the `title` field you have added to your `_index.md` file instead. You can access this value using the [`.GetPage` function][getpage]:
{{< code file="layouts/partials/by-page-field.html" >}}
<!-- Groups content according to content section.-->
{{ range .Data.Pages.GroupBy "Section" }}
<!-- Checks for existence of _index.md for a section; if available, pulls from "title" in front matter -->
{{ with $.Site.GetPage "section" .Key }}
<h3>{{.Title}}</h3>
{{ else }}
<!-- If no _index.md is available, ".Key" defaults to the section title and filters to title casing -->
<h3>{{ .Key | title }}</h3>
{{ end }}
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{< /code >}}
### By Date
{{< code file="layouts/partials/by-page-date.html" >}}
<!-- Groups content by month according to the "date" field in front matter -->
{{ range .Data.Pages.GroupByDate "2006-01" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{< /code >}}
### By Publish Date
{{< code file="layouts/partials/by-page-publish-date.html" >}}
<!-- Groups content by month according to the "publishdate" field in front matter -->
{{ range .Data.Pages.GroupByPublishDate "2006-01" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .PublishDate.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{< /code >}}
### By Page Parameter
{{< code file="layouts/partials/by-page-param.html" >}}
<!-- Groups content according to the "param_key" field in front matter -->
{{ range .Data.Pages.GroupByParam "param_key" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{< /code >}}
### By Page Parameter in Date Format
The following template takes grouping by `date` a step further and uses Golang's layout string. See the [`Format` function][] for more examples of how to use Golang's layout string to format dates in Hugo.
{{< code file="layouts/partials/by-page-param-as-date.html" >}}
<!-- Groups content by month according to the "param_key" field in front matter -->
{{ range .Data.Pages.GroupByParamDate "param_key" "2006-01" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{< /code >}}
### Reverse Key Order
Ordering of groups is performed by keys in alphanumeric order (AZ, 1100) and in reverse chronological order (i.e., with the newest first) for dates.
While these are logical defaults, they are not always the desired order. There are two different syntaxes to change Hugo's default ordering for groups, both of which work the same way.
#### 1. Adding the Reverse Method
```
{{ range (.Data.Pages.GroupBy "Section").Reverse }}
```
```
{{ range (.Data.Pages.GroupByDate "2006-01").Reverse }}
```
#### 2. Providing the Alternate Direction
```
{{ range .Data.Pages.GroupByDate "2006-01" "asc" }}
```
```
{{ range .Data.Pages.GroupBy "Section" "desc" }}
```
### Order Within Groups
Because Grouping returns a `{{.Key}}` and a slice of pages, all of the ordering methods listed above are available.
Here is the ordering for the example that follows:
1. Content is grouped by month according to the `date` field in front matter.
2. Groups are listed in ascending order (i.e., the oldest groups first)
3. Pages within each respective group are ordered alphabetically according to the `title`.
{{< code file="layouts/partials/by-group-by-page.html" >}}
{{ range .Data.Pages.GroupByDate "2006-01" "asc" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages.ByTitle }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{< /code >}}
## Filter and Limiting Lists
Sometimes you only want to list a subset of the available content. A common is to only display “Posts” on blog's homepage. You can accomplish this with the `where` function.
### `where`
`where` works in a similar manner to the [`where` keyword in SQL][wherekeyword]. It selects all elements of the array or slice that match the provided field and value. `where` takes three arguments:
1. `array` *or* `slice of maps or structs`
2. `key` *or* `field name`
3. `match value`
{{< code file="layouts/_default/.html" >}}
{{ range where .Data.Pages "Section" "post" }}
{{ .Content }}
{{ end }}
{{< /code >}}
You can see more examples in the [functions documentation for `where`][wherefunction].
### `first`
`first` works in a similar manner to the [`limit` keyword in SQL][limitkeyword]. It reduces the array to only the `first N` elements. It takes the array and number of elements as input. `first` takes two arguments:
1. `array` *or* `slice of maps or structs`
2. `number of elements`
{{< code file="layout/_default/section.html" >}}
{{ range first 10 .Data.Pages }}
{{ .Render "summary" }}
{{ end }}
{{< /code >}}
### `first` and `where` Together
Using `first` and `where` together can be very powerful:
{{< code file="first-and-where-together.html" >}}
<!-- Orders the content inside the "posts" section by the "title" field and then ranges through only the first 5 posts -->
{{ range first 5 (where .Data.Pages "Section" "post").ByTitle }}
{{ .Content }}
{{ end }}
{{< /code >}}
[base]: /templates/base/
[bepsays]: http://bepsays.com/en/2016/12/19/hugo-018/
[directorystructure]: /getting-started/directory-structure/
[`Format` function]: /functions/format/
[front matter]: /content-management/front-matter/
[getpage]: /functions/getpage/
[homepage]: /templates/homepage/
[homepage]: /templates/homepage/
[limitkeyword]: https://www.techonthenet.com/sql/select_limit.php
[mentalmodel]: http://webstyleguide.com/wsg3/3-information-architecture/3-site-structure.html
[pagevars]: /variables/page/
[partials]: /templates/partials/
[RSS 2.0]: http://cyber.law.harvard.edu/rss/rss.html "RSS 2.0 Specification"
[rss]: /templates/rss/
[sections]: /content-management/sections/
[sectiontemps]: /templates/section-templates/
[sitevars]: /variables/site/
[taxlists]: /templates/taxonomy-templates/#taxonomy-list-templates/
[taxterms]: /templates/taxonomy-templates/#taxonomy-terms-templates/
[taxvars]: /variables/taxonomy/
[views]: /templates/views/
[wherefunction]: /functions/where/
[wherekeyword]: https://www.techonthenet.com/sql/where.php