Year posts in routes.yaml

Hello

I would like to show the posts of a given year when a user goes for exemple to https://…/year/2021

So I added those lines in the routes.yaml

routes:
  /year/2019/:
    controller: channel
    filter: published_at:>='2019-01-01'+published_at:<='2019-12-31'
    order: published_at asc

  /year/2020/:
    controller: channel
    filter: published_at:>='2020-01-01'+published_at:<='2020-12-31'
    order: published_at asc

  /year/2021/:
    controller: channel
    filter: published_at:>='2021-01-01'+published_at:<='2021-12-31'
    order: published_at asc

It’s working well, but I want this feature for the last 30 years…

So I tried another approach:

I created a custom-year.hbs template with:

(...)

{{#post}}
  {{#get "posts" filter="published_at:>='{{title}}-01-01'+published_at:<='{{title}}-12-31'" visibility="all" limit="all"}}
        <li>{{posts.length}} posts this year so far ({{date format="YYYY"}})</li>

    <div class="post-feed">
            {{#foreach posts visibility="all"}}
                {{> "post-card"}}
            {{/foreach}}
    </div>
    {{/get}}
{{/post}}

And If I create a page named “2021” with this template, it works.

But still, I need to create 30 pages.

Is there a way to use the routes.yaml with a {slug} to do that ?

In the second option, I just enter the year one time in the title of the post (three time in routes).
And I can customize the page with dedicated title and number of posts.

So, I’m using second option for now, but it’s not very convenient as I have 30 pages to create.

Here is an update of the template

<main id="site-main" class="site-main outer" role="main">
  <div class="inner">
    <header class="post-full-header">
      <h1 class="post-full-title">{{post.title}}</h1>
    </header>

    {{#post}}
      {{#get "posts" filter="published_at:>='{{title}}-01-01'+published_at:<='{{title}}-12-31'" visibility="all" limit="all"}}
          {{posts.length}} posts
          <div class="post-feed">
              {{#if @member}}
                  {{#foreach posts visibility="all"}}
                      {{> "post-card"}}
                  {{/foreach}}
              {{else}}
                  {{#foreach posts }}
                      {{> "post-card"}}
                  {{/foreach}}
              {{/if}}
          </div>
        {{/get}}
    {{/post}}
  </div>
</main>