Range with 'first' and conditions

Hi,
I want to range over an array of events ( ‘nextDates’ ) and print out the first 4 that are

  • in the future
  • are not ‘full’

The array ‘nextDates’ is structured like this:

- 2:
    date: 2023-02-28T18:00:00+01:00
    weekday: Dienstag
    full: false
  - 3:
    date: 2023-04-11T18:00:00+01:00
    weekday: Dienstag
    full: true
  - 4:
    date: 2023-05-23T18:00:00+01:00
    weekday: Dienstag
    full: false

So far I’ve been printing out all events like this:

{{ range .Params.nextDates }}
  {{ if (and ((time .date).After now) (not .full)) }}
    <span>{{ dateFormat "2.1.2006" .date }}</span>
  {{ end }}
{{ end }}

I am struggling with limiting the output to only the first 4.

1 Like

Build a slice, then limit the slice.

{{ $events := slice }}
{{ range where .Params.nextDates "full" false }}
  {{ if ((time .date).After now) }}
    {{ $events = $events | append . }}
  {{ end }}
{{ end }}

{{ range $events | first 4 }}
  <span>{{ dateFormat "2.1.2006" .date }}</span>
{{ end }}
1 Like

Thank you, jmooring! :pray::+1:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.