* add generic menu declaration * update about section * commands * update content management section * update contribute section * functions * update getting started section * update hosting and deployment section * fix mailing list entry (no menu) * update news section * update Templates section * update tools section * update themes section * troubleshooting * update variables section Fixes rdwatters/hugo-docs-concept#45 See PR rdwatters/hugo-docs-concept#62
16 KiB
title, linktitle, description, date, publishdate, lastmod, categories, tags, menu, weight, draft, aliases, toc
| title | linktitle | description | date | publishdate | lastmod | categories | tags | menu | 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 |
|
|
|
22 | false |
|
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.
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:
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:
Default Section Templates
layouts/_default/section.htmllayouts/_default/list.html
Default Taxonomy List Templates
layouts/_default/taxonomy.htmlthemes/<THEME>/layouts/_default/taxonomy.html
Adding Content 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 contains page metadata (i.e., front matter) and content. This model allows you to include list-specific front matter via .Params and also means that list templates (e.g., layouts/_default/list.html) also have access to all page variables.
Example Project Directory
The following is an example of a typical Hugo project directory:
.
├── config.toml
├── 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" }}
<main class="main">
<article>
<header>
<h1>{{.Title}}</h1>
</header>
{{.Content}}
</article>
<ul class="section-contents">
{{ range .Data.Pages }}
<li>
<a href="{{.Permalink}}">{{.Date.Format "2006-01-02"}} | {{.Title}}</a
</li>
{{ end }}
</ul>
</main>
{{ end }}
{{% /code %}}
This above will output the following HTML:
{{% code file="yoursite.com/post/index.html" copy="false" %}}
<!--all your baseof.html code-->
<main class="main">
<article>
<header>
<h1>My Golang Journey</h1>
</header>
<p>I decided to start learning Golang in March 2017.</p>
<p>Follow my journey through this new blog.</p>
</article>
<ul class="section-contents">
<li><a href="/post/post-01/">Post 1</a></li>
<li><a href="/post/post-02/">Post 2</a></li>
</ul>
</main>
<!--all your other baseof.html code-->
{{% /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="yoursite.com/quote/index.html" copy="false" %}}
<!--baseof.html code-->
<main class="main">
<article>
<header>
<h1>Quotes</h1>
</header>
</article>
<ul class="section-contents">
<li><a href="https://yoursite.com/quote/quotes-01/">Quote 1</a></li>
<li><a href="https://yoursite.com/quote/quotes-02/">Quote 2</a></li>
</ul>
</main>
<!--baseof.html code-->
{{% /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 is used for spf13.com. It makes use of partial templates. All examples use a view called either "li" or "summary."
{{% code file="layouts/section/post.html" %}}
{{ partial "header.html" . }}
{{ partial "subheader.html" . }}
<section id="main">
<div>
<h1 id="title">{{ .Title }}</h1>
<ul id="list">
{{ range .Data.Pages }}
{{ .Render "li"}}
{{ end }}
</ul>
</div>
</section>
{{ partial "footer.html" . }}
{{% /code %}}
Taxonomy Template
{{% code file="layouts/_default/taxonomies.html" download="taxonomies.html" %}}
{{ define "main" }}
<section id="main">
<div>
<h1 id="title">{{ .Title }}</h1>
{{ range .Data.Pages }}
{{ .Render "summary"}}
{{ end }}
</div>
</section>
{{ end }}
{{% /code %}}
Ordering Content
Hugo lists render the content based on metadata provided in the front matter..
Here are a variety of different ways you can order the content items in your list templates:
Default: Weight > Date
{{% code file="layouts/partials/order-default.html" %}}
<ul class="pages">
{{ range .Data.Pages }}
<li>
<h1><a href="{{ .Permalink }}">{{ .Title }}</a></h1>
<time>{{ .Date.Format "Mon, Jan 2, 2006" }}</time>
</li>
{{ end }}
</ul>
{{% /code %}}
By Weight
{{% code file="layouts/partials/by-weight.html" %}}
{{ range .Data.Pages.ByWeight }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
By Date
{{% code file="layouts/partials/by-date.html" %}}
{{ range .Data.Pages.ByDate }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
By Publish Date
{{% code file="layouts/partials/by-publish-date.html" %}}
{{ range .Data.Pages.ByPublishDate }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .PublishDate.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
By Expiration Date
{{% code file="layouts/partials/by-expiry-date.html" %}}
{{ range .Data.Pages.ByExpiryDate }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .ExpiryDate.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
By Last Modified Date
{{% code file="layouts/partials/by-last-mod.html" %}}
{{ range .Data.Pages.ByLastmod }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
By Length
{{% code file="layouts/partials/by-length.html" %}}
{{ range .Data.Pages.ByLength }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
By Title
{{% code file="layouts/partials/by-title.html" %}}
{{ range .Data.Pages.ByTitle }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
By Link Title
{{% code file="layouts/partials/by-link-title.html" %}}
{{ range .Data.Pages.ByLinkTitle }}
<li>
<a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ 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.
The below example sorts a list of posts by their rating.
{{% code file="layouts/partials/by-rating.html" %}}
{{ range (.Data.Pages.ByParam "rating") }}
<!-- ... -->
{{ end }}
{{% /code %}}
If the front matter field of interest is nested beneath another field, you can also get it:
{{% code file="layouts/partials/by-nested-param.html" %}}
{{ range (.Date.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 .Data.Pages.ByDate.Reverse }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
{{% /code %}}
Grouping 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" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{% /code %}}
By Page date
{{% code file="layouts/partials/by-page-date.html" %}}
{{ range .Data.Pages.GroupByDate "2006-01" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{% /code %}}
By Page publish date
{{% code file="layouts/partials/by-page-publish-date.html" %}}
{{ range .Data.Pages.GroupByPublishDate "2006-01" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .PublishDate.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{% /code %}}
By Page Param
{{% code file="layouts/partials/by-page-param.html" %}}
{{ range .Data.Pages.GroupByParam "param_key" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{% /code %}}
By Page Param in Date Format
{{% code file="layouts/partials/by-page-param-as-date.html" %}}
{{ range .Data.Pages.GroupByParamDate "param_key" "2006-01" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{% /code %}}
Reversing Key Order
The ordering of the groups is performed by keys in alphanumeric order (A–Z, 1–100) and in reverse chronological order (newest first) for dates.
While these are logical defaults, they are not always the desired order. There are two different syntaxes to change the order, both of which work the same way. You can use your preferred syntax.
Reverse Method
{{ range (.Data.Pages.GroupBy "Section").Reverse }}
{{ range (.Data.Pages.GroupByDate "2006-01").Reverse }}
Providing the Alternate Direction
{{ range .Data.Pages.GroupByDate "2006-01" "asc" }}
{{ range .Data.Pages.GroupBy "Section" "desc" }}
Ordering Within Groups
Because Grouping returns a {{.Key}} and a slice of pages, all of the ordering methods listed above are available.
In the following example, groups are ordered chronologically and then content within each group is ordered alphabetically by title.
{{% code file="layouts/partials/by-group-by-page.html" %}}
{{ range .Data.Pages.GroupByDate "2006-01" "asc" }}
<h3>{{ .Key }}</h3>
<ul>
{{ range .Pages.ByTitle }}
<li>
<a href="{{ .Permalink }}">{{ .Title }}</a>
<div class="meta">{{ .Date.Format "Mon, Jan 2, 2006" }}</div>
</li>
{{ end }}
</ul>
{{ end }}
{{% /code %}}
Filtering and Limiting Lists
Sometimes you only want to list a subset of the available content. A common request is to only display “Posts” on the homepage. You can accomplish this with the where function.
where
where works in a similar manner to the where keyword in SQL. It selects all elements of the array or slice that match the provided field and value. where takes three arguments:
arrayor aslice of maps or structskeyorfield namematch value
{{% code file="layouts/_default/.html" %}}
{{ range where .Data.Pages "Section" "post" }}
{{ .Content }}
{{ end }}
{{% /code %}}
first
first works in a similar manner to the limit keyword in SQL. It reduces the array to only the first N elements. It takes the array and number of elements as input. first takes two arguments:
arrayorslice of maps or structsnumber 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") }}
{{ .Content }}
{{ end }}
{{% /code %}}