"Automatically" reroute to subdirectory page?

I have the current directory structure:
└── /pages
── /01.home
│ ── /_news-feature
│ ── /_primary-feature
│ ── /_winners2019
── /02.winners
│ ── /2016
│ ── /2017
│ ── /2018
│ ── /2019
│ ── /2020
── /03.details
Instead of creating a page for the “02.winners” directory, I would prefer the “Winners” navigation link to go directly to the page in the latest directory. In this case “/2020”.

My goal is that the future, if I add a directory for “2021” winners, it will automatically become the landing page for “Winners”. Is this possible? Also, the content of those directories with years for names are galleries of headshots and text for about 50 winners. They’re set up as collections of those winners: each winner has a folder with markdown and image.

I’ve tried lots of different ways of “importing” the content from the page in the “2020” directory into a twig template, but I never get the desired result. I end up just getting the content in the markdown, which is nothing really, instead of the full twig-processed gallery.

Any help is appreciated.

Hi @dvdfox if I follow correctly you might find the redirect page frontmatter option helpful:

I use this feature to always automatically reroute to the subfolder when a parent folder (in this case a course page) is viewed, here is some sample frontmatter as an example:

title: 'CMPT 363-202'
redirect: '/202/home'

Hope the above helps!

Thanks, paulhibbitts. Your solution works, but I wanted to avoid having to hard code the redirect to make things easier for a non-coding client.

I figured it out, though. It all came down to rendering a collection within a collection: in this case, the content of my directory’s landing page is a one-item only collection of the children by directory name, in descending order. That targets the child directory that is the newest year. Then it was a matter of correctly targeting that directory’s collection to render, which was my hangup all along. My Twig template became:

{% for r in page.collection %}

{% for p in r.collection %}
… do stuff with the contents of the subdirectory that was targeted

And that did it. The contents of the directory that has the latest year is rendered on the 2. winners landing page.

If I delete the 2020 directory, the page uses the 2019 directory. When I add a 2021 directory, it will automatically use that one.

Funny how the solutions to our problems come to us as we sleep on them.

Thanks again for your help. That’s a tip I’m sure will be useful in my Grav future!

1 Like

I had a similar problem and solved it with a collection with source

        - '@page.children': '/path/to/source'

instead of

        - '@self.children'

I didn’t need it, but then a redirect_me() could help, see Redirecting a page in the template - #2 by Muut