HUGO

Unable to compare dates stored in front matter

So, the below code allows me to compare a date once it’s hardcoded (i.e. 2021-09-11) but not if it’s stored in a variable.

I get a return of it being a string comparison, though all dates are stored as dates.

  {{ $reportDate := .Date }}
  {{ $pages := where site.RegularPages.ByTitle.Reverse "Section" .Section }}
  
  {{ $fatalityDate := 0 }}
  {{ range $pages }}
    {{ range $i, $e := .Params.report.reportedFatalities }}
        {{ $fatalityDate := .fatality.date }}

        {{ if eq $fatalityDate "2021-09-11" }}
        <ul>
          <li>
            <ul>
              <li>{{ .fatality.age }}</li>
              <li>{{ .fatality.sex }}</li>
              <li>{{ .fatality.island }}</li>
              <li>{{ $fatalityDate }}</li>
            </ul>
          </li>
        </ul>
        {{ end }}
      
    {{ end }}
  {{end}}

  

These work fine:

{{ $reportDate.Equal (time "2021-09-09") }}
{{ $reportDate.Equal $reportDate }}
{{ if eq $fatalityDate "2021-09-11" }}

But when I get into the range that I need to check, this works:

{{ if eq $fatalityDate "2021-09-11" }}

But this returns empty:

{{ if eq $fatalityDate $date }}

And this throws an error:

$fatalityDate.Equal (time "2021-11-04")

<$fatalityDate.Equal>: can't evaluate field Equal in type string

I don’t understand why it’s a string in one instance and then not in others…

It seems the code works fine enough besides, but can’t seem to render the comparison with dates stored in front matter…

If anyone has had experience with this before it would be greatly appreciated.

@bep @jmooring

Also, having gotten the new listing of dates, is it possible to get the length of the new array?

Along the lines of:

{{ range where $fatalityDate "eq" "2021-11-04"}}{{ len $fatalityDate }}{{end}}

So, I got the first bit solved by formatting .Date:

{{ $reportDate := .Date.Format "2006-01-02" }}

Tried this ditty to get the length of the new array:

{{ len (where . $fatalityDate "eq" $reportDate) }}

Got a number, unfortunately, it was 0.

There also seems to be an issue with the data filter, when listed, objects with the same value are merged. Any idea why this might be?

$fatalityDate

This is a string, not a date. So you can’t compare. Transform it into a date and you should be able to compare. Something like this (not tested) should work:

{{ $fatalityDate := $fatalityDate | time.Format "2006-01-02" }}
1 Like

Hey @davidsneighbour, I got it resolved by formatting the date.

Curious as to why it would be a string. Is it because it’s not ISO?

Also, would you happen to know how to get the length of the new array generated?

Everything you get from the front matter is a string is what I learned the hard way (except integers, and even those I would always format to get the proper types).

About the length… if it’s a formatted date string, shouldn’t it always be 10 characters long?

1 Like

I’m listing items of the same date, so I would like the length of the items in the new array: i.e. if there are 5 items dated 2021-11-30, I’d like to return that length.

check the length of that array (slice in golang) BEFORE you start the range.

{{ $arraylength := len .Params.report.reportedFatalities }}
{{ range .Params.report.reportedFatalities }}
... etc ...

Hey @davidsneighbour, that returns an array length that is correct for the file, but not for the filtered dates.

So, there are four (4) listed without being filtered and that’s correct, but not for the filtered date which is one (1).

Since, the new listing is being taken from the entire section and then filtered, shouldn’t there a be a qualifier and would it work something like this:

{{ range where . $fatalityDate "eq" $reportDate }}
    {{ len .Params.report.reportedFatalities }}
  {{end}}

Or thereabouts?

…and no, this did not work. At all. :sweat_smile:

So, I can also use the .fatality inside the .reportedFatalities array to return a length. Still, returning four (4), though.

I think this is half the battle. So, what I need is:

  • Get the list of $fatalityDate equal to $reportDate (done)
  • When this list is returned, I need the length of how many .fatality objects were returned