hugo Section functionality

hugo has the Section that provides functionality for categorizing articles.

Directories located directly under the contents/ directoryare considered as Sections.
In a straightforward usage, organizing articles into folders and having them appear in the corresponding path after building the site leads to intuitive behavior.

Sections can be specified as parameters in .Site.RegularPages and similar contexts. If you want to procedurally retrieve a list of articles, you can specify the section name like so: range .Site.RegularPages "Section" "<some-section>".

Sub-section

As described in the official documentation above, you can organize articles using nested subsections, such as contents/<some-section>/<sub-section>/. However, besides creating folders, you also need to have a <sub-section>/_index.md file.

While Hugo’s list pages render according to the list.html template, you can render sub-sections using the common list.html as well, and it will display the list of articles within <sub-section>/ as expected.

Subsections are not Sections

While the list of sub-sections is automatically extracted, the section of articles within <some-section>/<sub-section>/ becomes the top-level <some-section>.

Therefore, when extracting using range .Site.RegularPages "Section" "<some-section>" as mentioned earlier, the behavior will retrieve articles from descendant folders of <some-section> in a flat manner.

There isn’t a direct idiom to manipulate sub-sections.

To manipulate the set of articles within a sub-section, you can use .Site.GetPage to retrieve the list page and utilize the .Pages collection:

  {{ with .Site.GetPage "page" "/<some-section>/<sub-section>/" }}
      {{ range .Pages }}
            <!-- Render each Page -->
      {{ end }}
  {{ end }}
⁋ May 13, 2024↻ Oct 4, 2024