| 1 |
--- |
| 2 |
title: Sections |
| 3 |
description: Organize content into sections. |
| 4 |
|
| 5 |
categories: [] |
| 6 |
keywords: [] |
| 7 |
aliases: [/content/sections/] |
| 8 |
--- |
| 9 |
|
| 10 |
## Overview |
| 11 |
|
| 12 |
{{% glossary-term "section" %}} |
| 13 |
|
| 14 |
```tree |
| 15 |
content/ |
| 16 |
├── articles/ <-- section (top-level directory) |
| 17 |
│ ├── 2022/ |
| 18 |
│ │ ├── article-1/ |
| 19 |
│ │ │ ├── cover.jpg |
| 20 |
│ │ │ └── index.md |
| 21 |
│ │ └── article-2.md |
| 22 |
│ └── 2023/ |
| 23 |
│ ├── article-3.md |
| 24 |
│ └── article-4.md |
| 25 |
├── products/ <-- section (top-level directory) |
| 26 |
│ ├── product-1/ <-- section (has _index.md file) |
| 27 |
│ │ ├── benefits/ <-- section (has _index.md file) |
| 28 |
│ │ │ ├── _index.md |
| 29 |
│ │ │ ├── benefit-1.md |
| 30 |
│ │ │ └── benefit-2.md |
| 31 |
│ │ ├── features/ <-- section (has _index.md file) |
| 32 |
│ │ │ ├── _index.md |
| 33 |
│ │ │ ├── feature-1.md |
| 34 |
│ │ │ └── feature-2.md |
| 35 |
│ │ └── _index.md |
| 36 |
│ └── product-2/ <-- section (has _index.md file) |
| 37 |
│ ├── benefits/ <-- section (has _index.md file) |
| 38 |
│ │ ├── _index.md |
| 39 |
│ │ ├── benefit-1.md |
| 40 |
│ │ └── benefit-2.md |
| 41 |
│ ├── features/ <-- section (has _index.md file) |
| 42 |
│ │ ├── _index.md |
| 43 |
│ │ ├── feature-1.md |
| 44 |
│ │ └── feature-2.md |
| 45 |
│ └── _index.md |
| 46 |
├── _index.md |
| 47 |
└── about.md |
| 48 |
``` |
| 49 |
|
| 50 |
The example above has two top-level sections: articles and products. None of the directories under articles are sections, while all of the directories under products are sections. A section within a section is a known as a nested section or subsection. |
| 51 |
|
| 52 |
## Explanation |
| 53 |
|
| 54 |
Sections and non-sections behave differently. |
| 55 |
|
| 56 |
|Sections|Non-sections |
| 57 |
:--|:-:|:-: |
| 58 |
Directory names become URL segments|:heavy_check_mark:|:heavy_check_mark: |
| 59 |
Have logical ancestors and descendants|:heavy_check_mark:|:x: |
| 60 |
Have list pages|:heavy_check_mark:|:x: |
| 61 |
|
| 62 |
With the file structure from the [example above](#overview): |
| 63 |
|
| 64 |
1. The list page for the articles section includes all articles, regardless of directory structure; none of the subdirectories are sections. |
| 65 |
1. The articles/2022 and articles/2023 directories do not have list pages; they are not sections. |
| 66 |
1. The list page for the products section, by default, includes product-1 and product-2, but not their descendant pages. To include descendant pages, use the `RegularPagesRecursive` method instead of the `Pages` method in the _section_ template. |
| 67 |
1. All directories in the products section have list pages; each directory is a section. |
| 68 |
|
| 69 |
## Template selection |
| 70 |
|
| 71 |
Hugo has a defined [lookup order][] to determine which template to use when rendering a page. The [lookup rules][] consider the top-level section name; subsection names are not considered when selecting a template. |
| 72 |
|
| 73 |
With the file structure from the [example above](#overview): |
| 74 |
|
| 75 |
Content directory|Section template |
| 76 |
:--|:-- |
| 77 |
`content/products`|`layouts/products/section.html` |
| 78 |
`content/products/product-1`|`layouts/products/section.html` |
| 79 |
`content/products/product-1/benefits`|`layouts/products/section.html` |
| 80 |
|
| 81 |
Content directory|Page template |
| 82 |
:--|:-- |
| 83 |
`content/products`|`layouts/products/page.html` |
| 84 |
`content/products/product-1`|`layouts/products/page.html` |
| 85 |
`content/products/product-1/benefits`|`layouts/products/page.html` |
| 86 |
|
| 87 |
If you need to use a different template for a subsection, specify `type` and/or `layout` in front matter. |
| 88 |
|
| 89 |
## Ancestors and descendants |
| 90 |
|
| 91 |
A section has one or more ancestors (including the home page), and zero or more descendants. With the file structure from the [example above](#overview): |
| 92 |
|
| 93 |
```text |
| 94 |
content/products/product-1/benefits/benefit-1.md |
| 95 |
``` |
| 96 |
|
| 97 |
The content file (benefit-1.md) has four ancestors: benefits, product-1, products, and the home page. This logical relationship allows us to use the `Parent` and `Ancestors` methods to traverse the site structure. |
| 98 |
|
| 99 |
For example, use the `Ancestors` method to render breadcrumb navigation. |
| 100 |
|
| 101 |
```go-html-template {file="layouts/_partials/breadcrumb.html"} |
| 102 |
<nav aria-label="breadcrumb" class="breadcrumb"> |
| 103 |
<ol> |
| 104 |
{{ range .Ancestors.Reverse }} |
| 105 |
<li> |
| 106 |
<a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> |
| 107 |
</li> |
| 108 |
{{ end }} |
| 109 |
<li class="active"> |
| 110 |
<a aria-current="page" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a> |
| 111 |
</li> |
| 112 |
</ol> |
| 113 |
</nav> |
| 114 |
``` |
| 115 |
|
| 116 |
With this CSS: |
| 117 |
|
| 118 |
```css |
| 119 |
.breadcrumb ol { |
| 120 |
padding-left: 0; |
| 121 |
} |
| 122 |
|
| 123 |
.breadcrumb li { |
| 124 |
display: inline; |
| 125 |
} |
| 126 |
|
| 127 |
.breadcrumb li:not(:last-child)::after { |
| 128 |
content: "»"; |
| 129 |
} |
| 130 |
``` |
| 131 |
|
| 132 |
Hugo renders this, where each breadcrumb is a link to the corresponding page: |
| 133 |
|
| 134 |
```text |
| 135 |
Home » Products » Product 1 » Benefits » Benefit 1 |
| 136 |
``` |
| 137 |
|
| 138 |
[lookup order]: /templates/lookup-order/ |
| 139 |
[lookup rules]: /templates/lookup-order/#lookup-rules |