Redirecting based on template

Hey guys! I am finishing up building a small page builder system with Kirby, where subpages in page serve as a building block. However, I have a small problem with that. In Kirby’s CMS, site.com/page/subpage is a separate page, wherein I’d need to redirect it to site.com/page#subpage.

How could I check page intendedTemplate before loading and alter URL loaded based on it? I’ve tried routes, yet unsuccessfully

I use routes for this. The alternative would be to set up templates, but that seems a waste. Could you post the code you tried?

when doing redirect in template do go($page->parent()->url().'#'.$page->slug());

when doing the route solution you also need to not redirect aka visit pages that did match the route but not the intendedTemplate.

My current idea is something like that:

    'routes' => [
       [
        'pattern' => '(:any)',
        'action'  => function($path) {
            $page = page($path);

            if (strpos($page->intendedTemplate(), "element_") !== false){
            	$current_url = $page->url();
            	$position = strrpos($current_url, "/");

            	$new_url = substr_replace($current_url, "#", $position, strlen($current_url));
            	return go($new_url, 301);
            }
        }
    ]

I use element_ to distinguish builder blocks. However the problem seems to be that if I enable multilanguage and url becomes /en/page/ instead of /page/, let’s say, it doesn’t “catch” the path and returns null

Or maybe my current path is wrong, I’d appreciate ideas

It’d work, yet I’d have to create a template for each builder block, which doesn’t seem to be the most efficient way to me

Yes, in multi-language environments your pattern has to catch the language code as well, otherwise it won’t work.

Your pattern is not quite correct, I think, or rather, it depends on your site structure. Currently, it will only catch first level pages, but you want to redirect subpages, not first-level pages. And then it depends if these element subpages are subpages of a first level page or if they can also be further down the tree

I see. What would be a good for a universal pattern? Or should I create a new one for each level in the tree?

You can fetch anything with the (:all) pattern. But I’d start at the second level, because the first level is not affected, so that would be (:any)/(:all) plus the language code.

Do you use the language code for all languages? Or do you omit it for the default language?