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

08 Nov 2021

211108-1405 Hugo create shortcode or template for Day

Goal: convert “2010-01-01” into “Day 1234”.

First tried to create a Hugo shortode, but you can’t use a shortcode inside a template:

Process: loading templates: ".../index.html:84:1": parse failed: template: index.html:84: unexpected "<" in command

Next step - a partial template! To call them one uses {{ partial templatename .}}, with . being the “context”. I passed .Key, that has the groupBy date, and it works.

So, the partial template day.html does ugly math to get the number of days since the first day of 2019:

{{ $date := (printf . | time) }}
{{ $startUnix := (printf "2019-01-01" | time) }}
{{ $diff := sub $date.Unix $startUnix.Unix }}
{{ $diffInDays := div $diff 86400}}
{{ $diffInDays }}

Then I use it inside templates like this:

<h2 class="title day">
{{ partial "day.html" .Key }}
Nel mezzo del deserto posso dire tutto quello che voglio.