--- title: Lists of content in Hugo linkTitle: List templates description: 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. categories: [templates] keywords: [lists,sections,rss,taxonomies,terms] menu: docs: parent: templates weight: 60 weight: 60 aliases: [/templates/list/,/layout/indexes/] toc: true --- ## 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][homepage]. 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: * [Home page](/templates/homepage) * [Section pages](/templates/section-templates) * [Taxonomy pages](/templates/taxonomy-templates) * [Taxonomy term pages](/templates/taxonomy-templates) * [RSS feeds](/templates/rss) * [Sitemaps](/templates/sitemap-template) For template lookup order, see [Template Lookup](/templates/lookup-order/). The idea of a list page comes from the [hierarchical mental model of the web][mentalmodel] and is best demonstrated visually: [![Image demonstrating a hierarchical website sitemap.](site-hierarchy.svg)](site-hierarchy.svg) ## List defaults ### Default templates Since section lists and taxonomy lists (N.B., *not* [taxonomy terms lists][taxterms]) are both *lists* with regards to their templates, both have the same terminating default of `_default/list.html` or `themes//layouts/_default/list.html` in their lookup order. In addition, both [section lists][sectiontemps] and [taxonomy lists][taxlists] have their own default list templates in `_default`. See [Template Lookup Order](/templates/lookup-order/) for the complete reference. ## Add content and front matter to list pages Since v0.18, [everything in Hugo is a `Page`][bepsays]. This means list pages and the homepage can have associated content files (i.e. `_index.md`) that contain page metadata (i.e., front matter) and content. This new model allows you to include list-specific front matter via `.Params` and also means that list templates (e.g., `layouts/_default/list.html`) have access to all [page variables][pagevars]. {{% note %}} It is important to note that all `_index.md` content files will render according to a *list* template and not according to a [single page template](/templates/single-page-templates/). {{% /note %}} ### Example project directory The following is an example of a typical Hugo project directory's content: ```txt . ... ├── 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 }}
{{ end }} {{< /code >}} This above will output the following HTML: {{< code file="example.com/posts/index.html" copy=false >}}

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" copy=false >}}

Quotes

{{< /code >}} {{% note %}} The default behavior of Hugo is to pluralize list titles; hence the inflection of the `quote` section to "Quotes" when called with the `.Title` [page variable](/variables/page/). You can change this via the `pluralizeListTitles` directive in your [site configuration](/getting-started/configuration/). {{% /note %}} ## Example list templates ### Section template This list template has been modified slightly from a template originally used in [spf13.com](https://spf13.com/). It makes use of [partial templates][partials] for the chrome of the rendered page rather than using a [base template][base]. The examples that follow also use the [content view templates][views] `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 >}} ## Order content Hugo lists render the content based on metadata you provide in [front matter]. In addition to sane defaults, Hugo also ships with multiple methods to make quick work of ordering content inside list templates: ### Default: Weight > Date > LinkTitle > FilePath {{< code file="layouts/partials/default-order.html" >}} {{< /code >}} ### By weight Lower weight gets higher precedence. So content with lower weight will come first. {{< code file="layouts/partials/by-weight.html" >}} {{< /code >}} ### By date {{< code file="layouts/partials/by-date.html" >}} {{< /code >}} ### By publish date {{< code file="layouts/partials/by-publish-date.html" >}} {{< /code >}} ### By expiration date {{< code file="layouts/partials/by-expiry-date.html" >}} {{< /code >}} ### By last modified date {{< code file="layouts/partials/by-last-mod.html" >}} {{< /code >}} ### By length {{< code file="layouts/partials/by-length.html" >}} {{< /code >}} ### By title {{< code file="layouts/partials/by-title.html" >}} {{< /code >}} ### By link title {{< code file="layouts/partials/by-link-title.html" >}} {{< /code >}} ### By page parameter Order based on the specified front matter parameter. Content that does not have the specified front matter field will use the site's `.Site.Params` default. If the parameter is not found at all in some entries, those entries will appear together at the end of the ordering. {{< code file="layouts/partials/by-rating.html" >}} {{ range (.Pages.ByParam "rating") }} {{ end }} {{< /code >}} If the targeted front matter field is nested beneath another field, you can access the field using dot notation. {{< code file="layouts/partials/by-nested-param.html" >}} {{ range (.Pages.ByParam "author.last_name") }} {{ end }} {{< /code >}} ### Reverse order Reversing order can be applied to any of the above methods. The following uses `ByDate` as an example: {{< code file="layouts/partials/by-date-reverse.html" >}} {{< /code >}} ## Group content Hugo provides some functions for grouping pages by Section, Type, Date, etc. ### By page field {{< code file="layouts/partials/by-page-field.html" >}} {{ range .Pages.GroupBy "Section" }}

{{ .Key }}

{{ end }} {{< /code >}} In the above example, you may want `{{ .Title }}` to point the `title` field you have added to your `_index.md` file instead. You can access this value using the [`.GetPage` function][getpage]: {{< code file="layouts/partials/by-page-field.html" >}} {{ range .Pages.GroupBy "Section" }} {{ with $.Site.GetPage "section" .Key }}

{{ .Title }}

{{ else }}

{{ .Key | title }}

{{ end }} {{ end }} {{< /code >}} ### By date {{< code file="layouts/partials/by-page-date.html" >}} {{ range .Pages.GroupByDate "2006-01" }}

{{ .Key }}

{{ end }} {{< /code >}} {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [`time.Format`] and the `.Key` in the result will be localized for the current language. ### By publish date {{< code file="layouts/partials/by-page-publish-date.html" >}} {{ range .Pages.GroupByPublishDate "2006-01" }}

{{ .Key }}

{{ end }} {{< /code >}} {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [`time.Format`] and the `.Key` in the result will be localized for the current language. ### By expiration date {{< code file="layouts/partials/by-page-expiry-date.html" >}} {{ range .Pages.GroupByExpiryDate "2006-01" }}

{{ .Key }}

{{ end }} {{< /code >}} {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [`time.Format`] and the `.Key` in the result will be localized for the current language. ### By last modified date {{< code file="layouts/partials/by-page-lastmod.html" >}} {{ range .Pages.GroupByLastmod "2006-01" }}

{{ .Key }}

{{ end }} {{< /code >}} {{< new-in "0.97.0" >}} `GroupByDate` accepts the same time layouts as in [`time.Format`] and the `.Key` in the result will be localized for the current language. ### By page parameter {{< code file="layouts/partials/by-page-param.html" >}} {{ range .Pages.GroupByParam "param_key" }}

{{ .Key }}

{{ end }} {{< /code >}} ### By page parameter in date format The following template takes grouping by `date` a step further and uses Go's layout string. See the [`Format` function] for more examples of how to use Go's layout string to format dates in Hugo. {{< code file="layouts/partials/by-page-param-as-date.html" >}} {{ range .Pages.GroupByParamDate "param_key" "2006-01" }}

{{ .Key }}

{{ end }} {{< /code >}} ### Reverse key order Ordering of groups is performed by keys in alphanumeric order (A–Z, 1–100) and in reverse chronological order (i.e., with the newest first) for dates. While these are logical defaults, they are not always the desired order. There are two different syntaxes to change Hugo's default ordering for groups, both of which work the same way. #### 1. Adding the reverse method ```go-html-template {{ range (.Pages.GroupBy "Section").Reverse }} ``` ```go-html-template {{ range (.Pages.GroupByDate "2006-01").Reverse }} ``` #### 2. Providing the alternate direction ```go-html-template {{ range .Pages.GroupByDate "2006-01" "asc" }} ``` ```go-html-template {{ range .Pages.GroupBy "Section" "desc" }} ``` ### Order within groups Because Grouping returns a `{{ .Key }}` and a slice of pages, all the ordering methods listed above are available. Here is the ordering for the example that follows: 1. Content is grouped by month according to the `date` field in front matter. 2. Groups are listed in ascending order (i.e., the oldest groups first) 3. Pages within each respective group are ordered alphabetically according to the `title`. {{< code file="layouts/partials/by-group-by-page.html" >}} {{ range .Pages.GroupByDate "2006-01" "asc" }}

{{ .Key }}

{{ end }} {{< /code >}} ## 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. [base]: /templates/base/ [bepsays]: https://bepsays.com/en/2016/12/19/hugo-018/ [directorystructure]: /getting-started/directory-structure/ [`Format` function]: /functions/format/ [front matter]: /content-management/front-matter/ [getpage]: /functions/getpage/ [homepage]: /templates/homepage/ [mentalmodel]: https://webstyleguide.com/wsg3/3-information-architecture/3-site-structure.html [pagevars]: /variables/page/ [partials]: /templates/partials/ [RSS 2.0]: https://cyber.harvard.edu/rss/rss.html [rss]: /templates/rss/ [sections]: /content-management/sections/ [sectiontemps]: /templates/section-templates/ [sitevars]: /variables/site/ [taxlists]: /templates/taxonomy-templates/#taxonomy-list-templates [taxterms]: /templates/taxonomy-templates/#taxonomy-terms-templates [taxvars]: /variables/taxonomy/ [views]: /templates/views/ [`where`]: /functions/collections/where [`first]: /functions/first/ [main sections]: /functions/collections/where#mainsections [`time.Format`]: /functions/time/format