hugo/docs/content/en/methods/page/Section.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

1.3 KiB

title description categories keywords action
Section Returns the name of the top level section in which the given page resides.
related returnType signatures
methods/page/Type
string
PAGE.Section

With this content structure:

content/
├── lessons/
│   ├── math/
│   │   ├── _index.md
│   │   ├── lesson-1.md
│   │   └── lesson-2.md
│   └── _index.md
└── _index.md

When rendering lesson-1.md:

{{ .Section }} → lessons

In the example above "lessons" is the top level section.

The Section method is often used with the where function to build a page collection.

{{ range where .Site.RegularPages "Section" "lessons" }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}

This is similar to using the Type method with the where function

{{ range where .Site.RegularPages "Type" "lessons" }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}

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.