Trying to filter data files based upon a date field


#1

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

Thoughts?


#2

Read the TOM (or YAML) spec. That aren’t dates. That are strings.


#3

Huh. They’re the same format as in frontmatter in TOML in a post, so I figured they would work that way.

I just tried changing it to this:

startdate = 2016-05-12T23:29:49Z
enddate = 2016-05-13T23:29:49Z

Which matches the spec listed at https://github.com/toml-lang/toml#user-content-datetime and I get the same issue…

Now the output looks like this:

Chicago 2015-08-25 23:29:49 +0000 UTC
Kiel 2016-05-12 23:29:49 +0000 UTC
current date 2015-11-20 01:58:18.387917495 -0600 CST

FYI, here’s a link to the branch with the offending code. https://github.com/devopsdays/devopsdays-web/tree/filter-upcoming-events

The actual file where the search/filtering is happening is https://github.com/devopsdays/devopsdays-web/blob/filter-upcoming-events/themes%2Fdevopsdays%2Flayouts%2Findex.html


#4

Try

{{ if ge $e.startdate.Unix .Now.Unix }}

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.


#5

Want to see something REALLY weird now?

Updated the code thusly:

      {{ 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…


#6

That doesn’t make sense…

Anyhow, see


#7

OK, so if I want to move it to a where, it should look something like this? I know it’s not right, because it blows up, but am I getting close?

{{ range where .Site.Data.events "startdate" "ge" "Now.Unix" }}


#8

This should work:

{{ range where .Site.Data.events "startdate.Unix" "ge" .Now.Unix }}

In Hugo 0.15 this should also work:

{{ range where .Site.Data.events "startdate" "ge" .Now }}

Question: now in shortcodes?
#9

I tried that. Hugo throws this error:

ERROR: 2015/11/20 Error while rendering homepage: template: theme/index.html:24:15: executing "theme/index.html" at <where .Site.Data.eve...>: error calling where: can't iterate over map[string]interface {}