Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Reusable templates #1

Open
drewbaker opened this issue Jul 31, 2024 · 2 comments
Open

Feature request: Reusable templates #1

drewbaker opened this issue Jul 31, 2024 · 2 comments

Comments

@drewbaker
Copy link

drewbaker commented Jul 31, 2024

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:

// router.php
$router = new MakeitWorkPress\WP_Router\Router( 
    [
        'film'        => ['route' => film/, 'template' => 'video.php'],
        'tv'           => ['route' => tv/, 'template' => 'video.php'],
        'music'    => ['route' => music/] // defaults to key as template name, so music.php        
    ], 
);
// video.php
if( get_current_route() == 'film' ) {
    // Some code unique to film route
}

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.

@drewbaker drewbaker changed the title Feature request: Get active template name Feature request: Reusable templates Jul 31, 2024
@leichim
Copy link
Member

leichim commented Aug 7, 2024

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..

@drewbaker
Copy link
Author

drewbaker commented Aug 7, 2024

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/functions/libs/WpEasyRouter.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/'],
]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants