serhii.net

In the middle of the desert you can say anything you want

08 Nov 2021

211108-1246 Hugo groupBy to group stuff by days

Previously I had the posts split by days (“Day 1234”), now for every former h2-header I have a separate post, but still want to split them by days.

Hugo can group posts by stuff, including by dates. 1

This kinda works with pagination. 2

Now my list.html template for Diensttagebuch uses this to iterate through days/groups:

{{ $pages_k := where .RegularPagesRecursive ".Parent.Title" "Days" }} 
{{ $pages_j := where $pages_k "Params.draft" "ne" true}} 
{{ $pages_l := where $pages_j "Params.hidden" "ne" true}} 
{{ range (.Paginate ($pages_l.GroupByDate "2006-01-02")).PageGroups  }}

With the important bit being here, this iterates by day, not by month as in the examples: $pages_l.GroupByDate "2006-01-02"

Then the “day” header itself is {{.Key}}, to get the day of the month + month-year I do this:

<span class="day">{{ dateFormat "02" .Key }}</span>
{{ dateFormat "Jan 2006" .Key }}

Then iterating through the individual posts inside each “day” is:

{{ range .Pages }}
    <a href="{{ .RelPermalink }}">{{.Title}}</a>
    <span class="description">
    {{ .Content }}
    </span>
{{ end }}

  1. Everything that has to do with grouping and lists described here: Lists of Content in Hugo | Hugo↩︎

  2. Pagination | Hugo ↩︎

Nel mezzo del deserto posso dire tutto quello che voglio.
comments powered by Disqus