| 1 |
--- |
| 2 |
title: collections.Group |
| 3 |
description: Returns a map by grouping the given page collection (slice) by a specific key. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [group] |
| 9 |
returnType: page.PageGroup |
| 10 |
signatures: [collections.Group KEY PAGES] |
| 11 |
aliases: [/functions/group] |
| 12 |
--- |
| 13 |
|
| 14 |
```go-html-template |
| 15 |
{{ $new := .Site.RegularPages | first 10 | group "New" }} |
| 16 |
{{ $old := .Site.RegularPages | last 10 | group "Old" }} |
| 17 |
{{ $groups := slice $new $old }} |
| 18 |
{{ range $groups }} |
| 19 |
<h3>{{ .Key }}{{/* Prints "New", "Old" */}}</h3> |
| 20 |
<ul> |
| 21 |
{{ range .Pages }} |
| 22 |
<li> |
| 23 |
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> |
| 24 |
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div> |
| 25 |
</li> |
| 26 |
{{ end }} |
| 27 |
</ul> |
| 28 |
{{ end }} |
| 29 |
``` |
| 30 |
|
| 31 |
The page group you get from `group` is of the same type you get from the built-in [group methods][] in Hugo. The example above can be [paginated][]. |
| 32 |
|
| 33 |
[group methods]: /quick-reference/page-collections/#group |
| 34 |
[paginated]: /templates/pagination/ |