Nested where + date compare not working?

Hi,

I’m kinda blocked and lost with a nested where condition. Some help would be really helpul for I haven’t found any answers on the forum yet. :slight_smile:

Here’s what I’m trying to do:
Parse a type of content with a param event_date greater than now.

Here’s what I got:

---
title: "My title"
date: 2018-02-18T11:49:51+01:00
draft: false
event_info: 
 event_date: 2020-12-14 
---
    {{ range (where (where .Data.Pages "Section" "bal" ) (time (.Params.event_info.event_date)) "ge" $today ) }}
    {{ . }}
{{ end }}

returns nothing

If i try to count potential result with:

{{ len (where (where .Data.Pages "Section" "bal" ) (time (.Params.event_info.event_date)) "ge" now ) }}
  • This returns 0 instead of 1.
  • If I try with "le": return 0 instead of 8.
  • If I try with "!=": return 9 (correct result).

Any idea of what I’m missing ?
Thanks for your time :slight_smile:

System information:

  • Win7
  • Hugo v0.36.1

edit: tried with Hugo v0.37, same results.

Most likely ge, etc are math functions. I haven’t tried it, but first do .Unix on the date param and then compare them with now.Unix.

Update: No wonder this felt like deja vu (ref) :smile:

Update 2: Also, pretty sure that the 2nd and 3rd args of where have to be strings…

1 Like

Thanks for your time :slight_smile:

Here’s what I tried and error messages that I got:

len (where (where .Data.Pages "Section" "bal" ) "(time (.Params.event_info.event_date))" "ge" "now" ) 

=> error calling where: (time ( isn't a field of struct type *hugolib.Page
(I don’t really understand this error message btw)

len (where (where .Data.Pages "Section" "bal" ) (time (.Params.event_info.event_date)).Unix "ge" now.Unix

=> can't evaluate field Unix in type *errors.errorString

len (where (where .Data.Pages "Section" "bal" ) (dateFormat "20060102" .Params.event_info.event_date) "ge" (now.Format "20060102")) }}

=> executing "main" at <dateFormat "20060102...>: error calling dateFormat: unable to cast <nil> of type <nil> to Time

(time .Params.event_info.event_date).After now

This one, outside of any where returns the right answer. But if I call it within the where like this:

len (where (where .Data.Pages "Section" "bal" ) (time .Params.event_info.event_date).After now "eq" "true")

=> can't evaluate field After in type *errors.errorString

If I declare this as a string

"(time .Params.event_info.event_date).After now"`)

=> executing "main" at <where (where .Data.P...>: error calling where: (time isn't a field of struct type *hugolib.Page

len (where (where .Data.Pages "Section" "bal" ) (.Params.event_info.event_date | dateFormat "20060102") "ge" (now.Format "20060102"))

=> executing "main" at <dateFormat>: wrong number of args for dateFormat: want 2 got 1


I’m pretty sure I’m missing something quite easy, but don’t know what (I’m fairly new to Go and Hugo)

Bump :slight_smile:

Bump :slight_smile:

I landed here with the same (ok…similar) issue. I have not figured out how to do it with a compound where clause. In my case I am using Params and not the built in variables. This worked for me.
{{ range where (.Data.Pages.ByParam “eventdate”) “Section” “events” }}
{{ $now := now.Format “2006-01-02” }}
{{ $start := (dateFormat “2006-01-02” .Params.eventdate) }}
{{ if ge $start $now }}
do cool stuff here
{{ end }}
{{ end }}

1 Like

Was anyone able to get the length of the new array?