What I’m trying to do is show elements, in this case meetings, that I store as markdown files, in three categories - this month, upcoming, and previous. That’s relatively easy when I enumerate over the collection three times like that:
{{ range .Pages }}
{{ if lt ((time .Params.eventDate).Format "01.2006") (now.Format "01.2006") }}
<p class="fw-bolder fs-5 m-0 lh-1">{{ .Params.name }}</p>
{{ end }}
{{ end }}
However, what I’d also like to do, is show a message like “No meetings this month” when there’s no meetings in a category. Here’s what I thought should work, but I can’t figure out how to call the time function in this syntax:
{{ range where .Pages (gt ((time .Params.eventDate).Format "01.2006") (now.Format "01.2006")) }}
Here’s the error I get:
execute of template failed at <time .Params.eventDate>: error calling time: unable to cast <nil> of type <nil> to Time
Putting “” around .Params.eventDate doesn’t work either, then it says it can’t call time on this string. If I can figure out how to filter those events in the where clause, I suppose I can then assign the result to a variable and check the length somehow.
Is your front matter TOML or YAML? It makes a difference…
With TOML, date values are first-class citizens. TOML has a date data type while JSON and YAML do not. When you add a custom date field to your TOML front matter, the value is already of type time.Time and does not require conversion.
It’s YAML, but if it helps I can change to TOML or JSON. I could also use date in YAML, which I think is also automatically time type, but then the posts greater than current time won’t show at all. Also, I’m not sure if that’s the solution, as in the if clause it works perfectly - would it make a difference in a where clause?