| 1 |
--- |
| 2 |
title: Section |
| 3 |
description: Returns the name of the top-level section in which the given page resides. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [PAGE.Section] |
| 10 |
--- |
| 11 |
|
| 12 |
{{% glossary-term section %}} |
| 13 |
|
| 14 |
With this content structure: |
| 15 |
|
| 16 |
```tree |
| 17 |
content/ |
| 18 |
├── lessons/ |
| 19 |
│ ├── math/ |
| 20 |
│ │ ├── _index.md |
| 21 |
│ │ ├── lesson-1.md |
| 22 |
│ │ └── lesson-2.md |
| 23 |
│ └── _index.md |
| 24 |
└── _index.md |
| 25 |
``` |
| 26 |
|
| 27 |
When rendering lesson-1.md: |
| 28 |
|
| 29 |
```go-html-template |
| 30 |
{{ .Section }} → lessons |
| 31 |
``` |
| 32 |
|
| 33 |
In the example above "lessons" is the top-level section. |
| 34 |
|
| 35 |
The `Section` method is often used with the [`where`][] function to build a page collection. |
| 36 |
|
| 37 |
```go-html-template |
| 38 |
{{ range where .Site.RegularPages "Section" "lessons" }} |
| 39 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 40 |
{{ end }} |
| 41 |
``` |
| 42 |
|
| 43 |
This is similar to using the [`Type`][] method with the `where` function |
| 44 |
|
| 45 |
```go-html-template |
| 46 |
{{ range where .Site.RegularPages "Type" "lessons" }} |
| 47 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 48 |
{{ end }} |
| 49 |
``` |
| 50 |
|
| 51 |
However, if the `type` field in front matter has been defined on one or more pages, the page collection based on `Type` will be different than the page collection based on `Section`. |
| 52 |
|
| 53 |
[`Type`]: /methods/page/type/ |
| 54 |
[`where`]: /functions/collections/where/ |