hugo/content/en/templates/lists.md
Bjørn Erik Pedersen c2037f0c9a Squashed 'docs/' changes from 4f4d1f48c..acf70cdd9
acf70cdd9 Update CSS after removing Purge
74c0bb7f2 Remove purge from Webpack, temporarily
397b7284f Merge commit 'ccb1e38f3822e9b5e4fb89a296324daf3400f23b'
ccb1e38f3 Squashed 'themes/gohugoioTheme/' changes from 042112e2d..b7853ae70
bca84e914 Fix search box text being invisible in Safari
3f21d0957 FAQ: "this feature is not available in your current Hugo version"
d8b318f90 Remove Gulp from Victor Hugo description
09d9b4c24 Update contribute/development.md
4d0f6ba47 Theme Components Support on the Hugo Themes Site (#769)
f2ec3f2a6 Fix broken link 404
af82c56c5 config: Remove superflous param
8dc14f563 Update theme total on homepage (#765)
7838fd7e2 Update description of Atlas to reflect new changes
6abab23c6 update: total of hugo themes
1dba24fde Clarify: summaryLength uses words not characters
8c54f45d6 Add missing punctuation
d17000481 Remove Hugo Frontend caddy-plugin
a7094ae62 remove unneeded meta tags
f8b0f31c6 Fix typo in cross references
61781bab9 Fix: use gh shortcode correctly
357bfca43 Update files.md
d1d2406cc Updated Hugo convert to clarify for and not ..
86850f3c3 Fix typos in multilingual.md
b68d0d892 Fix typo
8cba70e28 Document disableAliases
00e440260 Remove extra whitespace
212d63e4c Update with latest Theme Submission Guidelines (#744)
c24ea638f Update frontends.md
942bc1972 Add one more video
2e1e1091d Update the rest of the videos
a7dc70f2b Add new installation video
1a78f8487 Hosting on AWS amplify
112420c46 Fix typo
f723425e4 Add fireship.io bio to showcase
574f43503 Fixed typo
88e0e0cb7 Release 0.54.0
303edf19e Merge branch 'temp54'
c38debebd releaser: Prepare repository for 0.55.0-DEV
5c6c56fa1 releaser: Add release notes to /docs for release of 0.54.0
f86e05950 releaser: Bump versions for release of 0.54.0
6a2684fe4 Add the default cache configuration
f75c0125a Merge commit 'f27faf9afd0a8db768a21954b8755f1bf1a14f1b'
e440dc6a4 Merge commit '5e078383a787e8b5ec3ba73f05ea4130840afbe2'
981bf53f8 Update _index.md
b54aea066 tpl: Fix reflect
41f8af5a6 Merge commit '978856e2ad12d2bcaf37bb9e31f806b30a4c42f4'

git-subtree-dir: docs
git-subtree-split: acf70cdd9fe2a89413e13db29466e45629372343
2019-03-26 18:41:09 +01:00

18 KiB
Raw Blame History

title linktitle description date publishdate lastmod categories keywords menu weight sections_weight draft aliases toc
Lists of Content in Hugo List Page 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. 2017-02-01 2017-02-01 2017-02-01
templates
lists
sections
rss
taxonomies
terms
docs
parent weight
templates 22
22 22 false
/templates/list/
/layout/indexes/
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.

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.

List Defaults

Default Templates

Since section lists and taxonomy lists (N.B., not taxonomy terms lists) are both lists with regards to their templates, both have the same terminating default of _default/list.html or themes/<THEME>/layouts/_default/list.html in their lookup order. In addition, both section lists and taxonomy lists have their own default list templates in _default:

See Template Lookup Order for the complete reference.

Add Content and Front Matter to List Pages

Since v0.18, everything in Hugo is a Page. 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.

{{% 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. {{% /note %}}

Example Project Directory

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" download="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. You can change this via the pluralizeListTitles directive in your site configuration. {{% /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" download="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" >}}

    {{ range .Pages }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

By Weight

Lower weight gets higher precedence. So content with lower weight will come first.

{{< code file="layouts/partials/by-weight.html" >}}

    {{ range .Pages.ByWeight }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

By Date

{{< code file="layouts/partials/by-date.html" >}}

    {{ range .Pages.ByDate }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

By Publish Date

{{< code file="layouts/partials/by-publish-date.html" >}}

    {{ range .Pages.ByPublishDate }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

By Expiration Date

{{< code file="layouts/partials/by-expiry-date.html" >}}

    {{ range .Pages.ByExpiryDate }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

By Last Modified Date

{{< code file="layouts/partials/by-last-mod.html" >}}

    {{ range .Pages.ByLastmod }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

By Length

{{< code file="layouts/partials/by-length.html" >}}

    {{ range .Pages.ByLength }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

By Title

{{< code file="layouts/partials/by-title.html" >}}

    {{ range .Pages.ByTitle }}
  • {{ .Title }}

  • {{ end }}
{{< /code >}}

{{< code file="layouts/partials/by-link-title.html" >}}

    {{ range .Pages.ByLinkTitle }}
  • {{ .LinkTitle }}

  • {{ end }}
{{< /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 (.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" >}}

    {{ range .Pages.ByDate.Reverse }}
  • {{ .Title }}

  • {{ end }}
{{< /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 }}

    {{ range .Pages }}
  • {{ .Title }}
    {{ .Date.Format "Mon, Jan 2, 2006" }}
  • {{ end }}
{{ 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:

{{< code file="layouts/partials/by-page-field.html" >}}

{{ range .Pages.GroupBy "Section" }}

{{ with $.Site.GetPage "section" .Key }}

{{.Title}}

{{ else }}

{{ .Key | title }}

{{ end }}
    {{ range .Pages }}
  • {{ .Title }}
    {{ .Date.Format "Mon, Jan 2, 2006" }}
  • {{ end }}
{{ end }} {{< /code >}}

By Date

{{< code file="layouts/partials/by-page-date.html" >}}

{{ range .Pages.GroupByDate "2006-01" }}

{{ .Key }}

    {{ range .Pages }}
  • {{ .Title }}
    {{ .Date.Format "Mon, Jan 2, 2006" }}
  • {{ end }}
{{ end }} {{< /code >}}

By Publish Date

{{< code file="layouts/partials/by-page-publish-date.html" >}}

{{ range .Pages.GroupByPublishDate "2006-01" }}

{{ .Key }}

    {{ range .Pages }}
  • {{ .Title }}
    {{ .PublishDate.Format "Mon, Jan 2, 2006" }}
  • {{ end }}
{{ end }} {{< /code >}}

By Page Parameter

{{< code file="layouts/partials/by-page-param.html" >}}

{{ range .Pages.GroupByParam "param_key" }}

{{ .Key }}

    {{ range .Pages }}
  • {{ .Title }}
    {{ .Date.Format "Mon, Jan 2, 2006" }}
  • {{ end }}
{{ 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 }}

    {{ range .Pages }}
  • {{ .Title }}
    {{ .Date.Format "Mon, Jan 2, 2006" }}
  • {{ end }}
{{ end }} {{< /code >}}

Reverse Key Order

Ordering of groups is performed by keys in alphanumeric order (AZ, 1100) 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 (.Pages.GroupBy "Section").Reverse }}
{{ range (.Pages.GroupByDate "2006-01").Reverse }}

2. Providing the Alternate Direction

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

{{ .Key }}

    {{ range .Pages.ByTitle }}
  • {{ .Title }}
    {{ .Date.Format "Mon, Jan 2, 2006" }}
  • {{ end }}
{{ 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 function and first function for further details.