Problems with formating custom front-matter dates in multilingual project

#1
Hugo Static Site Generator v0.55.0/extended darwin/amd64 BuildDate: unknown
GOOS="darwin"
GOARCH="amd64"
GOVERSION="go1.12.2"

Only built a few simple sites with Hugo and want to build a multilingual one. This is an event website so dates are pretty important. It’s also a multilingual setup with content in sub folders.

I only managed to format custom dates with dateFormat but the docs say it is not supported with multilingual setups …

I have the following custom dates in front-matter

---
date: "2019-03-03T14:00:00"
startDate: "2019-03-03T14:00:00"
endDate: "2019-03-03T18:00:00"
title: "this is a title in english"
slug: "this-test-event-english"
---

If I try to use .Format it does not work

{{ range where (.Pages.ByParam "startDate").Reverse "Type" "programme" }}
    <li>
      <p>{{ .Params.startDate.Format "January 2, 2006"  }}</p>
      <h3>{{ .Title }}</h3>
    </li>
{{ end }}

I have the following error
Error: Error building site: failed to render pages: render of "home" failed: execute of template failed: template: index.html:7:19: executing "main" at <.Params.startdate.format>: can't evaluate field format in type interface {}

The same code but using .Date works

{{ range where (.Pages.ByParam "startDate").Reverse "Type" "programme" }}
    <li>
      <p>{{ .Date.Format "January 2, 2006"  }}</p>
      <h3>{{ .Title }}</h3>
    </li>
{{ end }}

I can format my custom dates using dateFormat like so but the doc says it is not supported for multilingual setups anymore. The lack of multilingual support for dates in Go is also quite problematic and would force me to use .Format it seems.

{{ range where (.Pages.ByParam "startDate").Reverse "Type" "programme" }}
    <li>
      <p>{{ dateFormat "January 2, 2006" .Params.startDate }}</p>
      <h3>{{ .Title }}</h3>
    </li>
  {{ end }}

What am I supposed to do ?

This is really a bummer because, apart from that date parsing / localisation problem, the rest of the localisation functionalities in Hugo are pretty great …

0 Likes

#2

Hi there. It’s a shot in the dark, but try this

{{ (time .Params.startDate).Format "January 2, 2006" }}
0 Likes

#3

Thanks for helping out. Unfortunately, that does not seems to work, despite being something I didn’t think about trying ;o). I still have the following error

Error: Error building site: failed to render pages: render of "home" failed: execute of template failed: template: index.html:7:24: executing "main" at <.Params.startdate.format>: can't evaluate field format in type interface {}

0 Likes

#4

Hmm. Could you share your code?

0 Likes

#5

Sure, what I said above basically.
Just pushed a repo on Github https://github.com/jeromecoupe/hugo_multilingual

0 Likes

#6

Okay, got it to work. Interestingly, I had to refer to the param name in all lowercase, as well as pass the param to the time func.

{{ (time .Params.startdate).Format "January 2, 2006" }}

P.S. I tested this with Hugo v0.55.1

0 Likes

#7

Thanks for looking into it. Works on my end too.
Any idea as to why that parameter suddenly needs to be all lowercase ? Not a naming issue because I changed it for another name and same thing. Does Hugo only allow lowercase names in front matter ?

As I said above, I don’t have much experience with Go / Hugo but quite a bit with other templating languages and SSG.

I must say I find Hugo’s templating language a bit hard to grok, even borderline esoteric at times … like in this instance. Would you have any pointers you can recommend to make sense of it a bit more ?

0 Likes

#8

Ah think I fond it in the docs for user defined front matter variables

Page-level .Params are only accessible in lowercase.

That would explain it wouldn’t it ;o)

Many thanks again for your help with this. Much appreciated

1 Like

#9

TIL this as well

0 Likes

#10

Esoteric as I was saying ;o) Coming from templating languages like Liquid, Nunjucks, Twig, etc … this kind of stuff is a bit confusing. Anyway. Will try to finish this POC for a multilingual site in Hugo with Forestry as a Sass CMS for the client.

Many thanks again for looking into this and finding a solution!

0 Likes