hugo/docs/content/en/templates/lists/index.md
2024-06-21 09:41:24 +02:00

8 KiB

title linkTitle description categories keywords menu weight toc aliases
Lists of content in Hugo List templates Lists have a specific meaning and usage in Hugo when it comes to rendering your site homepage, section page, taxonomy list, or taxonomy terms list.
templates
lists
sections
rss
taxonomies
terms
docs
parent weight
templates 60
60 true
/templates/list/
/layout/indexes/

What is a list page template?

{{< youtube 8b2YTSMdMps >}}

A list page template is a template used to render multiple pieces of content in a single HTML page. The exception to this rule is the homepage, which is still a list but has its own dedicated template.

Hugo uses the term list in its truest sense; i.e. a sequential arrangement of material, especially in alphabetical or numerical order. Hugo uses list templates on any output HTML page where content is traditionally listed:

For template lookup order, see Template Lookup.

The idea of a list page comes from the hierarchical mental model of the web and is best demonstrated visually:

Image demonstrating a hierarchical website sitemap.

Add content and front matter to list pages

Add content and front matter to list pages by creating an _index.md file for home, section, taxonomy, and term pages.

The following is an example of a typical Hugo project directory's content:

.
...
├── content
|   ├── posts
|   |   ├── _index.md
|   |   ├── post-01.md
|   |   └── post-02.md
|   └── quote
|   |   ├── quote-01.md
|   |   └── quote-02.md
...

Using the above example, let's assume you have the following in content/posts/_index.md:

{{< code file=content/posts/_index.md >}}

title: My Go Journey date: 2017-03-23 publishdate: 2017-03-24

I decided to start learning Go in March 2017.

Follow my journey through this new blog. {{< /code >}}

You can now access this _index.md's' content in your list template:

{{< code file=layouts/_default/list.html >}} {{ define "main" }}

{{ .Title }}

{{ .Content }}
    {{ range .Pages }}
  • {{ .Date.Format "2006-01-02" }} | {{ .LinkTitle }}
  • {{ end }}
{{ end }} {{< /code >}}

This above will output the following HTML:

{{< code file=example.com/posts/index.html >}}

My Go Journey

I decided to start learning Go in March 2017.

Follow my journey through this new blog.

{{< /code >}}

List pages without _index.md

You do not have to create an _index.md file for every list page (i.e. section, taxonomy, taxonomy terms, etc) or the homepage. If Hugo does not find an _index.md within the respective content section when rendering a list template, the page will be created but with no {{ .Content }} and only the default values for .Title etc.

Using this same layouts/_default/list.html template and applying it to the quotes section above will render the following output. Note that quotes does not have an _index.md file to pull from:

{{< code file=example.com/quote/index.html >}}

Quotes

{{< /code >}}

{{% note %}} By default, Hugo capitalizes and pluralizes automatic list titles including section, taxonomy, and term pages. You can disable these transformations by setting capitalizeListTitles and pluralizeListTitles in your site configuration.

You can change the capitalization style in your site configuration to one of ap, chicago, go, firstupper, or none. See details.

{{% /note %}}

Example list templates

Section template

This list template has been modified slightly from a template originally used in spf13.com. It makes use of partial templates for the chrome of the rendered page rather than using a base template. The examples that follow also use the content view templates li.html or summary.html.

{{< code file=layouts/section/posts.html >}} {{ partial "header.html" . }} {{ partial "subheader.html" . }}

{{ .Title }}

    {{ range .Pages }} {{ .Render "li" }} {{ end }}
{{ partial "footer.html" . }} {{< /code >}}

Taxonomy template

{{< code file=layouts/_default/taxonomy.html >}} {{ define "main" }}

{{ .Title }}

{{ range .Pages }} {{ .Render "summary" }} {{ end }}
{{ end }} {{< /code >}}

Sort content

By default, Hugo sorts page collections by:

  1. Page weight
  2. Page date (descending)
  3. Page linkTitle, falling back to page title
  4. Page file path if the page is backed by a file

Change the sort order using any of the methods below.

{{< list-pages-in-section path=/methods/pages filter=methods_pages_sort filterType=include titlePrefix=. omitElementIDs=true >}}

Group content

Group your content by field, parameter, or date using any of the methods below.

{{< list-pages-in-section path=/methods/pages filter=methods_pages_group filterType=include titlePrefix=. omitElementIDs=true >}}

Filtering and limiting lists

Sometimes you only want to list a subset of the available content. A common is to only display posts from main sections on the blog's homepage.

See the documentation on where and first for further details.