Time function in range condition

I am building an event summary page that is driven by array info in /data/events.yml.

item  :
  - name     : This is the event title
    location : City, ST
    date     : Nov 17-22, 2019
    enddate  : 2019-11-22 #or 2019-11-22T16:00:00-07:00

  - name     : Another event title
    location : Another City, ST
    date     : Sept 1-2, 2019
    enddate  : 2019-09-02 #or 2019-09-02T16:00:00-07:00

Each event has an enddate string that will be used to calculate past or future status. The goal is to output the data in a list template file so it is grouped under future and past headings. Most of the examples I can find about conditional dates assume pages and frontmatter dates, so they do not apply to this case.

I am able to evaluate the status of each event within a range loop by converting to Unix format:
<p>Now is {{ now.Unix }}. Event date is: {{ (time .enddate).Unix }} </p>
<p>{{ if le (time .enddate) now }}past {{ else }}future {{ end }}event</p>

Problem: I am unable to evaluate the date in the range’s where clause. Example of what I am seeking:
{{ range where .Site.Data.events.item "(time .enddate).Unix" "lt" "now.Unix" }}

Also, before the loop starts (ahead of the range), I want to evaluate if there are any members of the array so I can output the appropriate future/past heading.

Thanks for any assistance!

An alternative way:

{{ $past_events := slice }}

{{ range site.Data.events.item }}
  {{ if le (time .enddate).Unix now.Unix }}
    {{ $past_events = $past_events | append . }}
  {{ end }}
{{ end }}

{{ range $past_events }}
...
1 Like

Thank you for providing a way to build subset before looping. That’s what I needed but wasn’t aware of the slice concept. The documentation for slice is quite bare, so your example was perfect! This is what I ended up with:

{{ $future_events := slice }}

{{ range site.Data.events.item }}
    {{ if ge (time .enddate).Unix now.Unix }}
        {{ $future_events = $future_events | append . }}
    {{ end }}
{{ end }}

{{ if gt (len $future_events) 0 }}
    <h2>Future events</h2>

    {{ range $future_events }}
    <div class="row" style="margin-bottom: 2em;">
        <div class="team-member center-align col-sm-3 col-lg-2">
                {{ $event :=  . }}
                {{ with .image }}
                <div class="team-img">
                    <img src="{{ . | absURL }}" class="team-pic" style="max-width: 160px;" alt="{{ $event.image_alt }}" title="{{ $event.image_alt }}">
                </div>
                {{ end }}
        </div>
        <div class="team-member col-sm-9 col-lg-10">
                {{ with .name }}<a id='{{ replace . " " "_" }}' style="position: absolute; left:0; top:-100px;"></a>{{ end }}
                {{ with .name }}<h3 class="team_name">{{ . }}</h3>{{ end }}
                <p class="team_title"> {{ with .date }}{{ . }} | {{ end }}{{ with .location }}{{ . }}{{ end }}</p>

                {{ with .desc }}<p class="team_text">{{ . }}</p>{{ end }}
        </div>
    </div>
    {{ end }}

{{ end }}
1 Like