Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

"Automatically" reroute to subdirectory page?

Solved by David Fox View solution

Started by David Fox 6 years ago · 3 replies · 749 views
6 years ago

I have the current directory structure:
/user
└── /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.

6 years ago

Hi @dvdfox if I follow correctly you might find the redirect page frontmatter option helpful:
https://learn.getgrav.org/16/content/headers#redirect

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:

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

Hope the above helps!

6 years ago Solution

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
3 years ago

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

YAML
content:
    items:
        - '@page.children': '/path/to/source'

instead of

YAML
content:
    items:
        - '@self.children'

I didn't need it, but then a redirect_me() could help, see /forum/archive/redirecting-a-page-in-the-template-t3517

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 44 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago