---
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.
You can now access this _index.md’s’ content in your list template:
{{define"main"}}<main><article><header><h1>{{.Title}}</h1></header><!-- "{{.Content}}" pulls from the markdown content of the corresponding _index.md -->{{.Content}}</article><ul><!-- 遍历 content/posts/*.md -->{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Date.Format"2006-01-02"}} | {{.Title}}</a></li>{{end}}</ul></main>{{end}}
上面模板会输出如下的HTML:
example.com/posts/index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!--top of your baseof code--><main><article><header><h1>My Go Journey</h1></header><p>I decided to start learning Go in March 2017.</p><p>Follow my journey through this new blog.</p></article><ul><li><ahref="/posts/post-01/">Post 1</a></li><li><ahref="/posts/post-02/">Post 2</a></li></ul></main><!--bottom of your baseof-->
<!--baseof--><main><article><header><!-- Hugo assumes that .Title is the name of the section since there is no _index.md content file from which to pull a "title:" field --><h1>Quotes</h1></header></article><ul><li><ahref="https://example.com/quote/quotes-01/">Quote 1</a></li><li><ahref="https://example.com/quote/quotes-02/">Quote 2</a></li></ul></main><!--baseof-->
Hugo列表list基于内容的前言设定 front matter中的元数据来生成内容. 除了缺省显示, hugo也提供了多个方法在list模板内对内容进行快速排序。
缺省的排序顺序: Weight > Date > LinkTitle > FilePath
layouts/partials/default-order.html
1
2
3
4
5
6
7
8
9
<ul>{{range.Pages}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
By Weight
越小的weight值有越高的优先级。具有低weight值的内容会先出现.
layouts/partials/by-weight.html
1
2
3
4
5
6
7
8
9
<ul>{{range.Pages.ByWeight}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
By Date
layouts/partials/by-date.html
1
2
3
4
5
6
7
8
9
10
<ul><!-- 基于前言设定中的“date”值对内容排序 -->{{range.Pages.ByDate}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
By Publish Date
layouts/partials/by-publish-date.html
1
2
3
4
5
6
7
8
9
10
<ul><!-- 基于前言设定中的“publishdate”值对内容排序-->{{range.Pages.ByPublishDate}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
By Expiration Date
layouts/partials/by-expiry-date.html
1
2
3
4
5
6
7
8
9
<ul>{{range.Pages.ByExpiryDate}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
按最后修改日期排序
layouts/partials/by-last-mod.html
1
2
3
4
5
6
7
8
9
10
<ul><!-- 基于前言设定中的“lastmod”值对内容排序-->{{range.Pages.ByLastmod}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
按内容长度升序排列
layouts/partials/by-length.html
1
2
3
4
5
6
7
8
9
10
<ul><!-- 按照内容长度升序排序(最短的内容最先出现)-->{{range.Pages.ByLength}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
按title排序
layouts/partials/by-title.html
1
2
3
4
5
6
7
8
9
10
<ul><!-- 按前言设定中"title"的升序排列-->{{range.Pages.ByTitle}}<li><h1><ahref="{{.Permalink}}">{{.Title}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
按链接Title排序
layouts/partials/by-link-title.html
1
2
3
4
5
6
7
8
9
10
<ul><!-- 按前言设定中"linktitle"的升序排列.如果未设定"LinkTitle",使用"title"作为"linktitle" -->{{range.Pages.ByLinkTitle}}<li><h1><ahref="{{.Permalink}}">{{.LinkTitle}}</a></h1><time>{{.Date.Format"Mon, Jan 2, 2006"}}</time></li>{{end}}</ul>
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.
layouts/partials/by-rating.html
1
2
3
4
5
<!-- Ranges through content according to the "rating" field set in front matter -->{{range(.Pages.ByParam"rating")}}<!-- ... -->{{end}}
If the targeted front matter field is nested beneath another field, you can access the field using dot notation.
<!-- Groups content according to content section.-->{{range.Pages.GroupBy"Section"}}<!-- Checks for existence of _index.md for a section; if available, pulls from "title" in front matter -->{{with$.Site.GetPage"section".Key}}<h3>{{.Title}}</h3>{{else}}<!-- If no _index.md is available, ".Key" defaults to the section title and filters to title casing --><h3>{{.Key|title}}</h3>{{end}}<ul>{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.Date.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{end}}
按日期分组 By Date
layouts/partials/by-page-date.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 基于前言设定的"date"中的月份将内容分组 -->{{range.Pages.GroupByDate"2006-01"}}<h3>{{.Key}}</h3><ul>{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.Date.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{end}}
按发布日期分组 By Publish Date
layouts/partials/by-page-publish-date.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 基于前言设定的"publishDate"中的月份将内容分组 -->{{range.Pages.GroupByPublishDate"2006-01"}}<h3>{{.Key}}</h3><ul>{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.PublishDate.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{end}}
按最后修改时间分组 By Lastmod
layouts/partials/by-page-lastmod.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- 基于前言设定的"lastMod"中的月份将内容分组 -->{{range.Pages.GroupByLastmod"2006-01"}}<h3>{{.Key}}</h3><ul>{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.Lastmod.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{end}}
按过期时间分组 By Expiry Date
layouts/partials/by-page-expiry-date.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Groups content by month according to the "expiryDate" field in front matter -->{{range.Pages.GroupByExpiryDate"2006-01"}}<h3>{{.Key}}</h3><ul>{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.ExpiryDate.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{end}}
基于页面参数分组 By Page Parameter
layouts/partials/by-page-param.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Groups content according to the "param_key" field in front matter -->{{range.Pages.GroupByParam"param_key"}}<h3>{{.Key}}</h3><ul>{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.Date.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{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.
layouts/partials/by-page-param-as-date.html
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- Groups content by month according to the "param_key" field in front matter -->{{range.Pages.GroupByParamDate"param_key""2006-01"}}<h3>{{.Key}}</h3><ul>{{range.Pages}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.Date.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{end}}
反转key顺序 Reverse Key Order
分组内排序基于键值的字母顺序,和日期的反向顺序(最新的最先出现)。
逻辑默认如此, 但有时并不总是想要的顺序。下面是两钟改变Hugo默认分组内排序的格式,效果一样:
1. 添加Reverse方法
1
{{ range (.Pages.GroupBy "Section").Reverse }}
1
{{ range (.Pages.GroupByDate "2006-01").Reverse }}
2. 提供不同的方向
1
{{ range .Pages.GroupByDate "2006-01" "asc" }}
1
{{ range .Pages.GroupBy "Section" "desc" }}
组内排序 Order Within Groups
对于内容分组返回的{{.Key}} 和页面list的切片, 上面提到的排序方法都适用:
下面例子中的排序:
内容按照前言设定中date的月份分组
不同组按照升序排列(最早的组先出来)
相应的每个组内页面通过title按字母顺序排序
layouts/partials/by-group-by-page.html
1
2
3
4
5
6
7
8
9
10
11
12
{{range.Pages.GroupByDate"2006-01""asc"}}<h3>{{.Key}}</h3><ul>{{range.Pages.ByTitle}}<li><ahref="{{.Permalink}}">{{.Title}}</a><divclass="meta">{{.Date.Format"Mon, Jan 2, 2006"}}</div></li>{{end}}</ul>{{end}}