按年份进行范围打印

huangapple go评论73阅读模式
英文:

Range through times to pretty print by year

问题

目前我正在使用https://play.golang.org/p/P1-sAo5Qy8这样的方式打印帖子的归档日期:

<ul>
<li><time datetime="2009-11-10 23:00:00 +0000 UTC">2009年11月10日</time>&raquo;<a href="/foo/bar">2009年发生了某事</a></li>
<li><time datetime="2005-11-10 23:00:00 +0000 UTC">2005年11月10日</time>&raquo;<a href="/foo/old">10年前发生了某事</a></li>
<li><time datetime="2009-06-10 23:00:00 +0000 UTC">2009年6月10日</time>&raquo;<a href="/foo/june">2009年夏天</a></li>
</ul>

不过我认为按年份打印会更好:

<h1>2009年</h1>
<ul>
<li><time datetime="2009-11-10 23:00:00 +0000 UTC">2009年11月10日</time>&raquo;<a href="/foo/bar">2009年发生了某事</a></li>
<li><time datetime="2009-06-10 23:00:00 +0000 UTC">2009年6月10日</time>&raquo;<a href="/foo/june">2009年夏天</a></li>
</ul>
<h1>2005年</h1>
<ul>
<li><time datetime="2005-11-10 23:00:00 +0000 UTC">2005年11月10日</time>&raquo;<a href="/foo/old">10年前发生了某事</a></li>
</ul>

我应该如何按照帖子的发布日期倒序排列,以打印我想要的分组?是否可以在模板中完成所有操作?

英文:

Currently I'm printing post archive dates like so with https://play.golang.org/p/P1-sAo5Qy8:

<ul>
<li><time datetime="2009-11-10 23:00:00 +0000 UTC">2009 Nov 10</time>&raquo;<a href="/foo/bar">Something happened in 2009</a></li>
<li><time datetime="2005-11-10 23:00:00 +0000 UTC">2005 Nov 10</time>&raquo;<a href="/foo/old">Something happened 10 years ago</a></li>
<li><time datetime="2009-06-10 23:00:00 +0000 UTC">2009 Jun 10</time>&raquo;<a href="/foo/june">Summer of 2009</a></li>
</ul>

Though I think it's nicer to print by year:

<h1>2009</h1>
<ul>
<li><time datetime="2009-11-10 23:00:00 +0000 UTC">2009 Nov 10</time>&raquo;<a href="/foo/bar">Something happened in 2009</a></li>
<li><time datetime="2009-06-10 23:00:00 +0000 UTC">2009 Jun 10</time>&raquo;<a href="/foo/june">Summer of 2009</a></li>
</ul>
<h1>2005</h1>
<ul>
<li><time datetime="2005-11-10 23:00:00 +0000 UTC">2005 Nov 10</time>&raquo;<a href="/foo/old">Something happened 10 years ago</a></li>
</ul>

How would I range reverse chronically over the Posts PostDate, to print the grouping that I want? Can it be done all in the template?

答案1

得分: 2

在你的Posts结构体上实现sort.Interface,然后按照逆序对其进行排序。

type Posts struct {
    Posts []Post
}

func (p Posts) Len() int {
    return len(p.Posts)
}

func (p Posts) Less(i, j int) bool {
    return p.Posts[i].PostDate.Before(p.Posts[j].PostDate)
}

func (p Posts) Swap(i, j int) {
    p.Posts[i], p.Posts[j] = p.Posts[j], p.Posts[i]
}

然后使用以下代码对其进行排序:

posts := Posts{p}
sort.Sort(sort.Reverse(posts))

这将按照你想要的顺序给出帖子。

接下来,你需要使用闭包实现一个函数,以便检查当前年份是否与上一篇帖子的年份相同,以实现按年份分组。如果是,则只输出帖子;否则,输出带有年份的标题,后跟帖子内容。

currentYear := "1900"
funcMap := template.FuncMap{
    "newYear": func(t string) bool {
        if t == currentYear {
            return false
        } else {
            currentYear = t
            return true
        }
    },
}

使用以下代码来使用它:

{{ range . }}{{ if newYear (.PostDate.Format "2006") }}<li><h1>{{ .PostDate.Format "2006" }}</h1></li>{{ end }}

Playground上查看一个可运行的示例

英文:

Implement the sort.Interface on your Posts struct, then sort it in reverse order.

type Posts struct {
	Posts []Post
}

func (p Posts) Len() int {
	return len(p.Posts)
}

func (p Posts) Less(i, j int) bool {
	return p.Posts[i].PostDate.Before(p.Posts[j].PostDate)
}

func (p Posts) Swap(i, j int) {
	p.Posts[i], p.Posts[j] = p.Posts[j], p.Posts[i]
}

and

posts := Posts{p}
sort.Sort(sort.Reverse(posts))

That will give you the posts in the sequence you want them.

Next you'll have to implement a func using a closure so you can check if the current year is the same as the one for the last post to get the grouping by year. If yes output just the post, otherwise output a header with the year followed by the post.

currentYear := &quot;1900&quot;
funcMap := template.FuncMap{
	&quot;newYear&quot;: func(t string) bool {
		if t == currentYear {
			return false
		} else {
			currentYear = t
			return true
		}
	},
}

and to use it:

{{ range . }}{{ if newYear (.PostDate.Format &quot;2006&quot;) }}&lt;li&gt;&lt;h1&gt;{{ .PostDate.Format &quot;2006&quot; }}&lt;/h1&gt;&lt;/li&gt;{{ end }}

See a working example on the Playground.

huangapple
  • 本文由 发表于 2015年5月17日 15:43:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/30284533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定