Limit number of events in range

I have a loop or range for a listing of events that looks like this:

{{ range where .Data.Pages.ByDate "Section" "events" }}
  {{ if ge .Date.Unix (now.AddDate 0 0 -1) }}
    {{ .Render "li"}}
  {{ end }}
{{ end }}

It is getting ALL of my upcoming events and ordering them by date, most recent to the furthest out.

What is the best way to limit the number of events in this range to 5?

Probably first.

You can try this:

 {{ $events := where .Site.RegularPages.ByDate "Section" "events" }}
  {{ range where $events "Date" "ge" (now.AddDate 0 0 -1) | first 4 }}
      {{ partial "event-card.html" . }}
  {{end}}
1 Like

That is perfect — thank you