Closed drewbaker closed 2 months ago
Cool, that's a good suggestion, and I think it should already be possible (maybe with a very small change). Let me test it.
The current route can be retrieved by using get_query_var('template'), unless you specify a different query_var in the constructor of the Router..
I ended up taking this concept and making my own router for my use case. I don't need the permalink creation, as my templates will match to real pages/posts in WordPress. I also implemented Express style PathToRegex syntax so it's eay easier to handle more complex routes.
You can check it out here: https://github.com/drewbaker/wp-easy/blob/main/includes/class-router.php
<?php
/*
* Define the templates to use, based on the valid WordPress routes.
*
* Syntax is similar to Express paths in Node
* The key is the route name, and the value is an array of [path, template]
* If no template set, the key is used as the template name.
*
* SEE https://github.com/gpolguere/path-to-regexp-php
*/
wp_easy_router([
'home' => ['path' => '/'],
'work' => ['path' => '/work/'],
'work-detail' => ['path' => '/work/:spot/', 'template' => 'work'],
'reel' => ['path' => '/reel/'],
]);
That is really nice, especially the ability to handle params such as :spot (or anything else) in the routes.
I was wondering though, what made you go creating a new framework based on the classic way of theming, instead of going full site editor?
Thanks @leichim! Well, for the work we do at Funkhaus we moved away from WordPress templating a long time ago. We use WP exclusively as a headless CMS now, often with a Nuxt frontend. I've found that the single-file-component approach of Vue is just so good to work with.
So when my brother, who is a digital Creative Director who knows the basics of HTML/CSS/PHP asked me to modernize his old WordPress boilerplate for him, I wanted something that used the SFC approach of Vue and the DX of Nuxt (or Next) but in a tech stack he knew and that didn't require a build step... I personally hate the way front-end development has devolved into these super complex build steps and integrated runtime environments.
There are plenty of "site builders" out there, like SquareSpace, Wix, Readymag and WebFlow etc... They are all great for designers who don't know much about code, and with clients that are very flexible. But those sites always look a bit janky and are brittle once the clients get their hands on them. Having said all of that, I've never even used the WordPress site builder... I just assumed it worked like a less-good version of SquareSpace... Always thought it was a weird direction for WP to go in...
Ah, I see! Headless is indeed a breeze, especially with Vue! . (imho with Vite the tooling/building/testing part became great too)
I'd love to be able to make templates reusable, and then also be able to get the name of the active template.
I think it can be done without a breaking change, and would make the router really really powerful!
So I could do something like this:
This would make my templates more reusable, and allow me to keep my route based logic all based on the config in my router.php file.