Confusing Hugo issues: datetime and 'isset'

Yesterday I got notified from Github for two opening issues of the Hugo’s theme hyde-hyde I have ported and developed further.

The newest one is reported by jdayton3 (Jonathan Dayton) on wrong dates shown up. The issue seems very strange and difficult to spot. It took me a while to research around and found some relevant reports by Paul Heinlein and Dana Woodman, that Hugo date/time formatting is internally based on Golang, and therefore, uses a smart but confusing convention. It’s funny that I made almost the same mistake as Dana (i.e. he used the format string “March 1, 2010” whilst mine is “Jan 1, 2006”). As such, the generated dates were totally incorrect.

Paul’s post summarise excellently the Go’s date/time format convention:

Format strings absolutely must adhere to the 1-2-3-4-5-6-7 order:

  • Month must be Jan, January, 01, or 1
  • Date must be 02 or 2
  • Hour must be 03, 3, or 15
  • Minute must 04
  • Second must be 05
  • Year must be 2006
  • Timezone must be MST or -7

After changing the format string, the aforementioned issue surely disappears. Even better, hyde-hyde date/time formatting can also now adopt a default option to prevent such potential issues.

{{ .Date.Format (.Site.Params.dateformat | default "Jan 02, 2006") }}

The other issue reported by paskal (Dmitry Verkhoturov) is an error Dmitry spotted when compiling and testing his blog against several Hugo themes including mine, hyde-hyde.

Building sites … ERROR 2018/03/20 20:35:56 Error while rendering "page" in "": template: /srv/hugo/themes/hyde-hyde/layouts/_default/single.html:6:7: executing "content" at <partial "post_conten...>: error calling partial: template: theme/partials/post_content.html:22:21: executing "theme/partials/post_content.html" at <len .Params.tags>: error calling len: len of untyped nil
ERROR 2018/03/20 20:35:56 Error while rendering "page" in "post/": template: /srv/hugo/themes/hyde-hyde/layouts/_default/single.html:6:7: executing "content" at <partial "post_conten...>: error calling partial: template: theme/partials/post_content.html:22:21: executing "theme/partials/post_content.html" at <len .Params.tags>: error calling len: len of untyped nil

The problem related to accessing the tags of each post via .Params.tags. The function len .Params.tags receives an untyped nil value. Strangely, there is a conditional check using the function isset right before that.

{{ if isset .Params "tags" }}
   {{ $total := len .Params.tags }}
   ...
{{ end }}

The semantics of isset is a bit vague:

Returns true if the parameter is set.

isset COLLECTION INDEX
isset COLLECTION KEY

It does not say anything when either COLLECTION or INDEX/KEY is nil or empty. As such, perhaps Dmitry’s blog contains some posts with empty tags. Hence, the variable .Params.tags yields "" whilst isset.Params "tags" evaluates to true.

One of the temporary mitigation is to use with instead. The semantics of with is a bit clearer.

Rebinds the context (.) within its scope and skips the block if the variable is absent.

Nevertheless, the error is dismissed when replacing the above code with the following. Hoooraaay!!!

{{ with .Params.tags }}
	{{ $total := len . }}
{{ end }}

Hugo is quite tricky, eh?

Related Articles

comments powered by Disqus