hugo/content/en/content-management/sections.md
Bjørn Erik Pedersen 77b976dd92 Squashed 'docs/' changes from a7e1e9be8..686c7b6eb
686c7b6eb ci(Netlify): specify `HUGO_VERSION` environment variable once
da99a356f fix: change heading level
e57da3f00 Update taxonomy methods
746172490 Update description of rendered breadcrumb navigation
6bc52fd40 Clarify term
dab07dcb0 Fix typo
e50fa452a Fix typo
6c1ea83c2 Update template overview page
a5dc97845 Clarify the append function
a135e52a0 Update GitHub hosting instructions
a51bf9f4f Update sections page
ed35fc6c4 Update archetypes and glossary
1a4522b3e Format examples
a70f20094 Use "hugo new content" to create content
673846ff9 Remove comment
b7febf0c5 Fix link
6f6fe2133 Miscellaneous edits
99227dd18 Remove lookup order table from output formats page
bc8870657 tools/editors: Add Prettier Plugin for Go Templates
157b169eb Update docs.yaml
1c8f514e0 Update cond function
e5f1f8113 Add assumptions to taxonomy and term template lookup order examples
475b406e2 Update postprocess
2d6cb8dfc glossary: Update content type
03b514bac Add descriptions to template lookup order example sections
06678f919 glossary: Fix broken link
4cd505612 Simplify news listing
fadb980db Update glossary of terms
491bacd78 Change order of example sections for template lookup order
04b8f39ec Create glossary of terms
12e896bc0 Remove reference to asciidoctor-rouge extension
055f7bb37 Insert missing words
8cd6ac387 Miscellaneous edits
2cbe17f41 Update configuration.md
529615373 Update data-templates.md
853154e65 Update theme
45f08627a resources.getRemote: Fix definition list
29a51dac1 Update docshelper
3bdfe88c6 Remove link to gitter from site footer
cacd0e461 Use "map" instead of "dictionary"
704dd5da6 Document the transform.Remarshal template function
e8d744951 Populate news section via GitHub releases API
3ff1118c7 Replace docs.json with docs.yaml
7726bbcac Use docs.json to generate default config throughout the site
57dca93df Use docs.json to generate default config for related content
74d5082c7 Add some .RenderShortcodes docs
cf5ab5062 netlify: Hugo 0.117.0
420f7aa69 Add all config to docshelper.json

git-subtree-dir: docs
git-subtree-split: 686c7b6eb182ed335dc94b3a0b80c564f7658380
2023-08-30 19:23:47 +02:00

5.9 KiB

title description categories keywords menu toc weight aliases
Sections Organize content into sections.
content management
lists
sections
content types
organization
docs
parent weight
content-management 120
true 120
/content/sections/

Overview

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. Section templates receive one or more page collections in context.

{{% note %}} Although top-level directories without _index.md files are sections, we recommend creating _index.md files in all sections. {{% /note %}}

A typical site consists of one or more sections. For example:

content/
├── articles/             <-- section (top-level directory)
│   ├── 2022/
│   │   ├── article-1/
│   │   │   ├── cover.jpg
│   │   │   └── index.md
│   │   └── article-2.md
│   └── 2023/
│       ├── article-3.md
│       └── article-4.md
├── products/             <-- section (top-level directory)
│   ├── 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
├── _index.md
└── about.md

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.

Explanation

Sections and non-sections behave differently.

Sections Non-sections
Directory names become URL segments ✔️ ✔️
Have logical ancestors and descendants ✔️
Have list pages ✔️

With the file structure from the example above:

  1. The list page for the articles section includes all articles, regardless of directory structure; none of the subdirectories are sections.

  2. The articles/2022 and articles/2023 directories do not have list pages; they are not sections.

  3. 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.

  4. 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.

With the file structure from the example above:

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.

Ancestors and descendants

A section has one or more ancestors (including the home page), and zero or more descendants. With the file structure from the example above:

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.

{{< code file="layouts/partials/breadcrumb.html" >}}

    {{ range .Ancestors.Reverse }}
  1. {{ .LinkTitle }}
  2. {{ end }}
  3. {{ .LinkTitle }}
{{< /code >}}

With this CSS:

.breadcrumb ol {
  padding-left: 0;
}

.breadcrumb li {
  display: inline;
}

.breadcrumb li:not(:last-child)::after {
  content: "»";
}

Hugo renders this, where each breadcrumb is a link to the corresponding page:

Home » Products » Product 1 » Benefits » Benefit 1