Paginate, filter older than today

Hello

I have a partial template with paginator. It is a list of events with custom event_time in frontmatter.

{{ $paginator := .Paginate (where .Site.RegularPages "Section" "actu" )
    8 }}

It is ordered by frontmatter event_time parametter.

{{ range ($paginator.Pages.ByParam "event_time") }}

I want to filter events with event_time older than today :

 {{ $time := (.Params.event_time | time.AsTime).Unix }} {{ if lt now.Unix
    $time }}

It is hiding the events older than today but the problem is that it keeps 8 items in the pagination when a post is older than today. It is hidden but the array (?) for pagination keeps this item so it only displays 7 items and so on when new events become older than today.

Is there a way to make this filtering at the paginator level and pull the items older than today out of the pagination instead of hiding it ?

Thank you for your support !

Is your front matter TOML, YAML, or JSON?
If TOML, is the value of event_time quoted or unquoted?

Time comparisons are much simpler with unquoted TOML dates. For example:

+++
title = 'Actu 1'
date = 2022-09-08T10:40:45-07:00
draft = false
event_time = 2022-09-05T10:40:21-07:00
+++

It is unquoted YAML.

---
title: Actu 1
event_time: 2022-11-05T20:30:00.000Z
---
{{/* Create slice of pages. */}}
{{ $p := slice }}
{{ range where site.RegularPages "Section" "actu" }}
  {{ if (.Params.event_time | time.AsTime).Before now }}
    {{ $p = $p | append . }}
  {{ end }}
{{ end }}
{{ $p = sort $p "Params.event_time" "asc" }}

{{/* Render links. */}}
{{ range (.Paginate $p).Pages }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}

{{/* Render navigation. */}}
{{ template "_internal/pagination.html" . }}

Note that we’re sorting by event_time as a string, not a time.Time value. This could be incorrect if the event_time format is inconsistent in front matter.

Again, this would be much simpler if the front matter were TOML, and the event_time unquoted, because TOML dates are unmarshalled to time.Time values.

1 Like

Thanks a lot !

Don’t now why but I had to use my if statement instead of yours.

{{ $time := (.Params.event_time | time.AsTime).Unix }} {{ if lt now.Unix
    $time }}

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