2019-10-21 04:22:28 -04:00
---
2023-07-29 05:15:54 -04:00
title: Sections
2023-08-30 13:23:47 -04:00
description: Organize content into sections.
2019-10-21 04:22:28 -04:00
categories: [content management]
keywords: [lists,sections,content types,organization]
menu:
docs:
2022-11-17 10:14:29 -05:00
parent: content-management
weight: 120
weight: 120
2023-12-04 09:14:18 -05:00
toc: true
2022-11-17 10:14:29 -05:00
aliases: [/content/sections/]
2019-10-21 04:22:28 -04:00
---
2023-08-30 13:23:47 -04:00
## Overview
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
A section is a top-level content directory, or any content directory with an _index.md file. A content directory with an _index.md file is also known as a [branch bundle ](/getting-started/glossary/#branch-bundle ). Section templates receive one or more page [collections ](/getting-started/glossary/#collection ) in [context ](/getting-started/glossary/#context ).
2019-10-21 04:22:28 -04:00
{{% note %}}
2023-08-30 13:23:47 -04:00
Although top-level directories without _index.md files are sections, we recommend creating _index.md files in _all_ sections.
2019-10-21 04:22:28 -04:00
{{% /note %}}
2023-08-30 13:23:47 -04:00
A typical site consists of one or more sections. For example:
```text
content/
├── articles/ < -- section ( top-level directory )
2023-12-04 09:14:18 -05:00
│ ├── 2022/
│ │ ├── article-1/
│ │ │ ├── cover.jpg
│ │ │ └── index.md
│ │ └── article-2.md
│ └── 2023/
│ ├── article-3.md
│ └── article-4.md
2023-08-30 13:23:47 -04:00
├── products/ < -- section ( top-level directory )
2023-12-04 09:14:18 -05:00
│ ├── product-1/ < -- section ( has _index . md file )
│ │ ├── benefits/ < -- section ( has _index . md file )
│ │ │ ├── _index.md
│ │ │ ├── benefit-1.md
│ │ │ └── benefit-2.md
│ │ ├── features/ < -- section ( has _index . md file )
│ │ │ ├── _index.md
│ │ │ ├── feature-1.md
│ │ │ └── feature-2.md
│ │ └── _index.md
│ └── product-2/ < -- section ( has _index . md file )
│ ├── benefits/ < -- section ( has _index . md file )
│ │ ├── _index.md
│ │ ├── benefit-1.md
│ │ └── benefit-2.md
│ ├── features/ < -- section ( has _index . md file )
│ │ ├── _index.md
│ │ ├── feature-1.md
│ │ └── feature-2.md
│ └── _index.md
2023-08-30 13:23:47 -04:00
├── _index.md
└── about.md
```
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
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.
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
## Explanation
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
Sections and non-sections behave differently.
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
||Sections|Non-sections
:--|:-:|:-:
Directory names become URL segments|:heavy_check_mark:|:heavy_check_mark:
Have logical ancestors and descendants|:heavy_check_mark:|:x:
Have list pages|:heavy_check_mark:|:x:
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
With the file structure from the [example above ](#overview ):
1. The list page for the articles section includes all articles, regardless of directory structure; none of the subdirectories are sections.
1. The articles/2022 and articles/2023 directories do not have list pages; they are not sections.
2023-12-04 09:14:18 -05:00
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` collection instead of the `.Pages` collection in the list template. See [details](/variables/page/#page-collections).
2023-08-30 13:23:47 -04:00
1. All directories in the products section have list pages; each directory is a section.
## Template selection
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.
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
With the file structure from the [example above ](#overview ):
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
Content directory|List page template
:--|:--
content/products|layouts/products/list.html
content/products/product-1|layouts/products/list.html
content/products/product-1/benefits|layouts/products/list.html
Content directory|Single page template
:--|:--
content/products|layouts/products/single.html
content/products/product-1|layouts/products/single.html
content/products/product-1/benefits|layouts/products/single.html
If you need to use a different template for a subsection, specify `type` and/or `layout` in front matter.
[lookup rules]: /templates/lookup-order/#lookup-rules
[lookup order]: /templates/lookup-order/
## Ancestors and descendants
2023-12-04 09:14:18 -05:00
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 ):
2023-08-30 13:23:47 -04:00
```text
content/products/product-1/benefits/benefit-1.md
```
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.
For example, use the `.Ancestors` method to render breadcrumb navigation.
2019-10-21 04:22:28 -04:00
2023-12-04 09:14:18 -05:00
{{< code file = layouts/partials/breadcrumb.html > }}
2023-08-30 13:23:47 -04:00
< nav aria-label = "breadcrumb" class = "breadcrumb" >
2023-05-22 10:43:12 -04:00
< ol >
{{ range .Ancestors.Reverse }}
< li >
2023-12-04 09:14:18 -05:00
< a href = "{{ .RelPermalink }}" > {{ .LinkTitle }}< / a >
2023-05-22 10:43:12 -04:00
< / li >
{{ end }}
< li class = "active" >
2023-12-04 09:14:18 -05:00
< a aria-current = "page" href = "{{ .RelPermalink }}" > {{ .LinkTitle }}< / a >
2023-05-22 10:43:12 -04:00
< / li >
< / ol >
< / nav >
2019-10-21 04:22:28 -04:00
{{< / code > }}
2023-08-30 13:23:47 -04:00
With this CSS:
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
```css
.breadcrumb ol {
padding-left: 0;
}
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
.breadcrumb li {
display: inline;
}
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
.breadcrumb li:not(:last-child)::after {
content: "»";
}
```
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
Hugo renders this, where each breadcrumb is a link to the corresponding page:
2019-10-21 04:22:28 -04:00
2023-08-30 13:23:47 -04:00
```text
Home » Products » Product 1 » Benefits » Benefit 1
```
2019-10-21 04:22:28 -04:00
[archetype]: /content-management/archetypes/
[content type]: /content-management/types/
[directory structure]: /getting-started/directory-structure/
[section templates]: /templates/section-templates/
2021-07-04 10:34:26 -04:00
[leaf bundles]: /content-management/page-bundles/#leaf-bundles
2019-10-21 04:22:28 -04:00
[branch bundles]: /content-management/page-bundles/#branch-bundles