Alongside posts I’m trying to build an events section on my hugo site, so far creating the events has gone well but now I’m trying to both group the events by day and filter them for those after today. So far I’ve only been able to either group them using code like this
range .Data.Pages.GroupByDate "Monday, 2 January 2006"
or else filter them using code like this
{{ range .Data.Pages }}
{{ if ge .Date.Unix .Now.Unix }}
has anyone got any ideas how I could do both at the same time?
We have a where function (http://gohugo.io/templates/functions/) which works well for this purpose, but only works on equality. Seems like it would be good do have a similar function that handled greater than. WhereGreater for example.
How about using both GroupByDate and ge like below?
{{ range .Data.Pages.GroupByDate "Monday, 2 January 2006" }}
{{ $page := index .Pages 0 }}{{/* Each group has at least 1 page */}}
{{ if ge $page.Date.Unix $page.Now.Unix }}
<h2>{{ .Key }}</h2>
<ul>
{{ range .Pages }}
{{ .Render li }}
{{ end }}
</ul>
{{ end }}
{{ end }}
Ordinary, GroupByDate returns sorted pages in descending order, means the newest one comes to the top of the list so you can easily determine whether the group should be displayed by only checking the first page date.
I agree with adding a new function which is similar to “where” but can handle other operation or it may be good “where” supports an operator argument like {{ where .Pages "Section" "=" "post" }}