39af43ef1 Update postprocess.md 3ec192d08 Update multilingual.md 7fc7bf862 Add a note about some changes in 0.112.0 742510ae8 Fix ordinal abbrev example fe557031a Correct spelling for 'GitHub' and 'GitLab' (#2082) 84a059b9a Fix typo in hosting-on-azure.md (#2080) 3383786fe Add i18n to list of directories affected by ignoreFiles 5bfb95234 Update 404.md (#2076) 87545a4fd Update hosting-on-cloudflare-pages.md (#2078) aa5952c28 Add default module mount to example (#2075) ced5292c8 Align permalinks examples (#2073) 77b5009fd Fix typo c79319a6a Clarify description of baseURL e93a9807b Fix typo in frontmatter description (#2071) 05fe9163a Remove erroneous statement aa59ef383 docs: Remove note about hugo server not using 404 (#2068) 4a387a6b8 Clarify findRESubmatch (#2065) 47a9181b5 Clarify findRE, replaceRE, and findRESubmatch (#2064) e5eedbb5e Update theme 5d392c3d4 Clarify pageRef menu property (#2059) a557b0ebf Fix typos on Configure Hugo page (#2058) 17ef283e6 Clarify module.replacements wording (#2052) 5db4aa421 Fixing broken links (#2057) 9afa0c2fa Fix broken links (#2055) 49b981b1f Correct repo URL for migration tool (contentful.com) (#2056) 969c24c16 Remove duplicate content 0b91e7676 Revert "Delete duplicate content" 3229e79f2 Delete duplicate content ec4eddb98 Fix typo 6509159d5 Describe snap package strict confinement (#2050) 1589bcdb7 Remove hugo.Generator admonition (#2048) 7e553d11b Add example 48bec0335 Replace blockquotes with admonitions where appropriate (#2043) 98226fe61 Remove orphaned param fron admonition calls (#2042) 2a37a1d21 Clarify cast functions (#2041) 03fd1d404 Fix typo 1898013ef Fix typos 944e27430 Replace output shortcode calls 0c66fb055 Add example of shortcode calls within sample code f25a79c69 Replace tip and warning shortcode calls 3afac22fc Refactor code shortcode ad65d2931 Clarify seq function 59f8a1f48 Clarify title function 47535dc87 Cleanup hasPrefix hasSuffix 7bee3e4c1 Cleanup action delimiters cc96070f0 Correct functions archetype ffe5d39b9 Remove duplicate shortcodes 075c9f3fe Remove old todos bc3ec033c Front matter cleanup (#2039) 928b94505 Add code fence types (#2038) 856fa293c Document .File.Filename (#2037) 0988c4a42 Update output-formats.md (#2036) 289da5658 Change findRe to findRE 1e50f0583 Update theme f90fb1bf5 Improve type formatting (#2032) 7785fa7d9 Use code-toggle shortcode where appropriate f11cabf37 Add space after and before action delimiters ac333c795 Replace erroneous use of nocopy shortcode param 064896c06 Use bool param when calling code-toggle fb33bf59b Update code-toggle shortcode 6ddeab4f8 Add missing go-html-template code fence type (#2030) 1bba4cefb Fix links (#2029) 77f4d6c32 Link destination cleanup (#2028) fc0ecc027 Improve breadcrumb example (#2026) 6148be2de Update the breadcrumb navigation example (#2025) 6ebb37b1b Clarify sort function (#2024) 31269bad9 Add Winget installation method (#1988) d6c5f940e Resource methods: add signatures, minor improvements (#2017) d2e594cbc Modify inner variable shortcode-template explanation (#1985) a54927a7f Update GitHub Pages starter workflow (#2023) 2964c2d44 Remove orphaned static files (#2022) 97e5567cc Complete documentation on '.Scratch' and '.Store' (#2016) fa7b2e299 Fix typo bdce77c57 Remove literal from example menu template c0f23b216 Correct and improve menu documentation (#2010) 464368fd9 Document .Page.Store (#2011) a3d7c4a3a Improve urls.Parse function (#2012) d2cec3776 Clarify postcss config option (#2013) eb3003fef Fixed typo (#2007) 90c82d7ea Clarify mermaid markdown example (#2004) 1b11dcd5c docs(Diagrams): Update mermaid import mechanism (#1967) 4aceb6855 Fingerprinting, asset management: minor improvements (#2003) bcbc519bb resources.GetRemote: minor improvement (#2002) d54185bef Clarify markdownify behavior (#1999) afb582a80 Clarify usage of slug in front matter (#1998) f71985315 Update hasSuffix.md 29ad622a3 netlify: Hugo 0.111.3 adf223ecc Merge branch 'tempv0.111.3' 06858c646 docs: Improve examples of variadic math functions 8b656994e tpl/math: Allow multi numbers in add, sub, mul, div, min and max 2a38c4046 tpl: Add hasSuffix alias 4e0b98d54 switch transfers to workers 11651ac0f customize parallel transfer count 142f5da81 Update GitHub hosting instructions (#1991) ad7901d2f netlify: Hugo 0.111.2 0651a76e0 add headings to distinguish render hook context params d96d75be4 netlify: Hugo 0.111.1 226cb9e3a Add a paragraph about the new page template function 4c0157a49 Add .Fragments docs 6c78c0679 netlify: Bump to Hugo 0.111.0 7b11c24cf Merge branch 'feat/related-fragments' 615d18ef8 Add Related fragments config a36449b0c cods: Regen docs helper 0272fa45f Merge commit '336622d5e7afd9334cd2de7150d4f16bdf7c24f9' c5a962b93 related: Add config option cardinalityThreshold f91677377 docs: Another fix related docs example 17aa939ea docs: Fix related docs example 12c449150 Merge commit 'cf591b7c0c598d34896709db6d28598da37e3ff6' cb998b3d6 Add page fragments support to Related git-subtree-dir: docs git-subtree-split: 39af43ef11c23b8eaea7e17b59ff065a169305ac
19 KiB
title | linktitle | description | categories | keywords | menu | weight | aliases | toc | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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. |
|
|
|
22 |
|
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:
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" >}} {{ define "main" }}
{{ .Title} }
{{ .Content }}-
{{ range .Pages }}
- {{ .Date.Format "2006-01-02" }} | {{ .Title }} {{ end }}
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.
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
{{% 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 }}
Taxonomy Template
{{< code file="layouts/_default/taxonomy.html" >}} {{ define "main" }}
{{ .Title }}
{{ range .Pages }} {{ .Render "summary" }} {{ end }}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 }}
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 }}
By Date
{{< code file="layouts/partials/by-date.html" >}}
-
{{ range .Pages.ByDate }}
-
{{ .Title }}
{{ end }}
By Publish Date
{{< code file="layouts/partials/by-publish-date.html" >}}
-
{{ range .Pages.ByPublishDate }}
-
{{ .Title }}
{{ end }}
By Expiration Date
{{< code file="layouts/partials/by-expiry-date.html" >}}
-
{{ range .Pages.ByExpiryDate }}
-
{{ .Title }}
{{ end }}
By Last Modified Date
{{< code file="layouts/partials/by-last-mod.html" >}}
-
{{ range .Pages.ByLastmod }}
-
{{ .Title }}
{{ end }}
By Length
{{< code file="layouts/partials/by-length.html" >}}
-
{{ range .Pages.ByLength }}
-
{{ .Title }}
{{ end }}
By Title
{{< code file="layouts/partials/by-title.html" >}}
-
{{ range .Pages.ByTitle }}
-
{{ .Title }}
{{ end }}
By Link Title
{{< code file="layouts/partials/by-link-title.html" >}}
-
{{ range .Pages.ByLinkTitle }}
-
{{ .LinkTitle }}
{{ end }}
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 }}
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 }}
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 }}
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 }}
{{< 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 }}
-
{{ range .Pages }}
-
{{ .Title }}
{{ .PublishDate.Format "Mon, Jan 2, 2006" }}
{{ end }}
{{< 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 Lastmod
{{< code file="layouts/partials/by-page-lastmod.html" >}}
{{ range .Pages.GroupByLastmod "2006-01" }}
{{ .Key }}
-
{{ range .Pages }}
-
{{ .Title }}
{{ .Lastmod.Format "Mon, Jan 2, 2006" }}
{{ end }}
{{< 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 Expiry Date
{{< code file="layouts/partials/by-page-expiry-date.html" >}}
{{ range .Pages.GroupByExpiryDate "2006-01" }}
{{ .Key }}
-
{{ range .Pages }}
-
{{ .Title }}
{{ .ExpiryDate.Format "Mon, Jan 2, 2006" }}
{{ end }}
{{< 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 }}
-
{{ range .Pages }}
-
{{ .Title }}
{{ .Date.Format "Mon, Jan 2, 2006" }}
{{ end }}
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 }}
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 (.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 the ordering methods listed above are available.
Here is the ordering for the example that follows:
- Content is grouped by month according to the
date
field in front matter. - Groups are listed in ascending order (i.e., the oldest groups first)
- 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 }}
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.