--- title: Lists of Content in Hugo linktitle: List Page 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. date: 2017-02-01 publishdate: 2017-02-01 lastmod: 2017-02-01 categories: [templates] #tags: [lists,sections,rss,taxonomies,terms] menu: docs: parent: "templates" weight: 22 weight: 22 sections_weight: 22 draft: false aliases: [/templates/list/,/layout/indexes/] toc: true --- ## What is a List Page Template? 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: * [Taxonomy terms pages][taxterms] * [Taxonomy list pages][taxlists] * [Section list pages][sectiontemps] * [RSS][rss] 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.](/images/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`: #### Default Section Templates 1. `layouts/_default/section.html` 2. `layouts/_default/list.html` #### Default Taxonomy List Templates 1. `layouts/_default/taxonomy.html` 2. `themes//layouts/_default/taxonomy.html` ## 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: ``` . ... ├── content | ├── post | | ├── _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/post/_index.md`: {{< code file="content/post/_index.md" >}} --- title: My Golang Journey date: 2017-03-23 publishdate: 2017-03-24 --- I decided to start learning Golang 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" download="list.html" >}} {{ define "main" }}

{{.Title}}

{{.Content}}
{{ end }} {{< /code >}} This above will output the following HTML: {{< code file="example.com/post/index.html" copy="false" >}}

My Golang Journey

I decided to start learning Golang 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 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](http://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/post.html" >}} {{ partial "header.html" . }} {{ partial "subheader.html" . }}

{{ .Title }}

    {{ range .Data.Pages }} {{ .Render "li"}} {{ end }}
{{ partial "footer.html" . }} {{< /code >}} ### Taxonomy Template {{< code file="layouts/_default/taxonomies.html" download="taxonomies.html" >}} {{ define "main" }}

{{ .Title }}

{{ range .Data.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 {{< 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 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 (.Data.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 (.Data.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 .Data.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 .Data.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 .Data.Pages.GroupByDate "2006-01" }}

{{ .Key }}

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

{{ .Key }}

{{ end }} {{< /code >}} ### By Page Parameter {{< code file="layouts/partials/by-page-param.html" >}} {{ range .Data.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 Golang's layout string. See the [`Format` function][] for more examples of how to use Golang's layout string to format dates in Hugo. {{< code file="layouts/partials/by-page-param-as-date.html" >}} {{ range .Data.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 ``` {{ range (.Data.Pages.GroupBy "Section").Reverse }} ``` ``` {{ range (.Data.Pages.GroupByDate "2006-01").Reverse }} ``` #### 2. Providing the Alternate Direction ``` {{ range .Data.Pages.GroupByDate "2006-01" "asc" }} ``` ``` {{ range .Data.Pages.GroupBy "Section" "desc" }} ``` ### Order Within Groups Because Grouping returns a `{{.Key}}` and a slice of pages, all of 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 .Data.Pages.GroupByDate "2006-01" "asc" }}

{{ .Key }}

{{ end }} {{< /code >}} ## Filter and Limiting Lists Sometimes you only want to list a subset of the available content. A common is to only display “Posts” on blog's homepage. You can accomplish this with the `where` function. ### `where` `where` works in a similar manner to the [`where` keyword in SQL][wherekeyword]. It selects all elements of the array or slice that match the provided field and value. `where` takes three arguments: 1. `array` *or* `slice of maps or structs` 2. `key` *or* `field name` 3. `match value` {{< code file="layouts/_default/.html" >}} {{ range where .Data.Pages "Section" "post" }} {{ .Content }} {{ end }} {{< /code >}} You can see more examples in the [functions documentation for `where`][wherefunction]. ### `first` `first` works in a similar manner to the [`limit` keyword in SQL][limitkeyword]. It reduces the array to only the `first N` elements. It takes the array and number of elements as input. `first` takes two arguments: 1. `array` *or* `slice of maps or structs` 2. `number of elements` {{< code file="layout/_default/section.html" >}} {{ range first 10 .Data.Pages }} {{ .Render "summary" }} {{ end }} {{< /code >}} ### `first` and `where` Together Using `first` and `where` together can be very powerful: {{< code file="first-and-where-together.html" >}} {{ range first 5 (where .Data.Pages "Section" "post").ByTitle }} {{ .Content }} {{ end }} {{< /code >}} [base]: /templates/base/ [bepsays]: http://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/ [homepage]: /templates/homepage/ [limitkeyword]: https://www.techonthenet.com/sql/select_limit.php [mentalmodel]: http://webstyleguide.com/wsg3/3-information-architecture/3-site-structure.html [pagevars]: /variables/page/ [partials]: /templates/partials/ [RSS 2.0]: http://cyber.law.harvard.edu/rss/rss.html "RSS 2.0 Specification" [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/ [wherefunction]: /functions/where/ [wherekeyword]: https://www.techonthenet.com/sql/where.php