Skip to content

Conversation

@ccjmne
Copy link
Contributor

@ccjmne ccjmne commented Nov 9, 2025

  • Are you doing the PR on the next branch?
  • Have you created/updated the relevant documentation page(s)?

Fixes #2619.

  • The "before" filter is exclusive, and the "after" one is inclusive; it
    may be better thought of as a "since" than a strict "after".
  • Pages with no dates are never included.
  • Dates can be 8601 default, like: 2023-12-01T12:00:00Z
    or only have the date component, like: 2023-12-01

@ccjmne
Copy link
Contributor Author

ccjmne commented Nov 9, 2025

With before being exclusive and after being inclusive, we have effectively access to the >= and < comparisons.

I figured it would be somewhat useful when honing in on a particular month or particular year:

  • this month's articles: section.pages | after(date="2025-11-01")
  • last year's articles: section.pages | after(date="2024-01-01") | before(date="2025-01-01")

@Keats
Copy link
Collaborator

Keats commented Nov 10, 2025

How about adding inclusive as param to both, defaulting to false so people can choose?

Filters an array of pages/sections to keep only items with a `date` field on or after the specified date. Items without a `date` field are excluded.

```jinja2
{% for page in section.pages | after(date="2020-03-10") %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at the usage, I'm wondering if it would be better to have the before/after work on a date rather than Page[]. It would be usable in more places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's a bit peculiar to have that filter work on an array of items that have a date property, that's pretty arbitrary.

To clarify you suggest we have some after and before filters that yield either true or false when they're used on a date, that we'd use like:

{% for page in section.pages %}
    {% if page.date | before(date="2025-01-01") %} {% continue %} {% endif %}
    {{ page.content | markdown | safe }}
{% endfor %}

Correct?
If so, that makes sense as well, I can do that :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't suppose it'd make sense to have it run on BOTH OF Page[]/Section[] and date, right? It'd be somewhat neat to have a filter that directly eliminates stuff from an array, I quite like the section.pages | after(xxx) construct.

But supporting both if page.date | before(xxx) and for page in section.page | before(xxx) is semantically overloaded, I believe.
It's very, very easy to implement and maintain, however ;)

I'll go ahead and assume you'd rather want only the:

{% if page.date | before(date="2025-01-01") %}

We can always revert that commit later anyway.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to work on a date rather than on a page/section. I think for that it will be better to design an improved filter filter for tera2 so you can apply a filter on every elements of a collection

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh hell yeah, a filter filter would be awesome :)

In the meantime, I went ahead and made the adjustments you requested. This approach will definitely be perfect once the filter filter is available, and it's already still quite useable.

Let me know how you want to proceed from here!

@ccjmne
Copy link
Contributor Author

ccjmne commented Nov 10, 2025

How about adding inclusive as param to both, defaulting to false so people can choose?

Ha! Yeah, seems obvious in retrospect. I'll do just that (tonight).

@ccjmne ccjmne requested a review from Keats November 12, 2025 15:22
@ccjmne
Copy link
Contributor Author

ccjmne commented Nov 24, 2025

Hey @Keats, I addressed your comments, do you think it makes sense to merge this as it now is?

@ccjmne ccjmne force-pushed the introduce-before-after-filters branch from f6557c7 to 2bfec1a Compare November 29, 2025 20:59
Fixes getzola#2619.

- The "before" filter is exclusive, and the "after" one is inclusive; it
  may be better thought of as a "since" than a strict "after".
- Pages with no dates are never included.
- Dates can be 8601 default,       like: 2023-12-01T12:00:00Z
  or only have the date component, like: 2023-12-01

Ref: getzola#2619
Change the default inclusiveness of "after" to be *false*, so that both
behave the same: exclusive by default, inclusive only if specifically
marked as such.

Suggested-by: Vincent Prouillet <[email protected]>
@ccjmne ccjmne force-pushed the introduce-before-after-filters branch from 2bfec1a to 6f72b54 Compare November 29, 2025 21:05
@Keats
Copy link
Collaborator

Keats commented Dec 3, 2025

I've just realised, these shouldn't be filters but tests

@ccjmne
Copy link
Contributor Author

ccjmne commented Dec 3, 2025

Ah, like xxx is defined? Is defined one such "test"?
I could look into having them be that, that does indeed make more sense :)

@Keats
Copy link
Collaborator

Keats commented Dec 3, 2025

Yes exactly, it's basically a shorthand for a function returning a bool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants