I am building a site that will have multiple events, and we need to be able to determine which events are current and which are in the past.
Since each event will have multiple pages associated with it, the desire is to use a data file for each event, which will then drive a bunch of templates.
I have tried putting in a startdate and enddate field in the TOML file for each event (I started by trying to do it with YAML, but then suspected that maybe hugo was having problems parsing what YAML thought was a date field, but this seems to be a red herring).
Here’s an example data file:
name = "kiel2016"
year = "2016"
city = "Kiel"
startdate = "2016-05-12T23:29:49-06:00"
enddate = "2016-05-13T23:29:49-06:00"
Then this is where I am attempting to simply display the events that are in the future:
{{ range $e := $.Site.Data.events }}
{{ if ge $e.startdate .Now }}
{{ .city }} {{ .startdate }}<br />
{{ end }}
{{ end }}
current date {{ .Now }}
The problem is, all events show up…
Chicago 2015-08-25T23:29:49-06:00
Kiel 2016-05-12T23:29:49-06:00
current date 2015-11-20 00:53:43.757705937 -0600 CST
ge and similar should support dates directly, I will try to remember to create an issue for that later …
For front matter Hugo parses the date strings into dates. Makes us more flexible with date formats (for people coming from Jekyl etc.). We can not do the same “guessing” for data files.
{{ range $e := $.Site.Data.events }}
{{ if ge .startdate.Unix .Now.Unix }}
{{ .city }} {{ .startdate.Unix }}<br />
{{ end }}
{{ end }}
current date {{ .Now.Unix }}
And the output looks like this:
Chicago 1440545389
Kiel 1463095789
current date 1448007493
So it’s clearly converting it into the Unix timeformat, but still not actually doing the logic. I feel like now I’m missing something simple. It would be even more clean if I could embed it all into a where clause in the initial select, but at this time of night my brain can’t parse it…