Brain-WP / Cortex

Routing system for WordPress
MIT License
348 stars 20 forks source link

Overriding `template_include` hook #14

Closed chrisvanpatten closed 8 years ago

chrisvanpatten commented 8 years ago

I'd like to override the template_include hook so I can integrate some basic template inheritance. However it doesn't seem possible, because you remove all other filters on the hook.

(Essentially I'm trying to always load a base.php template, then set the actual template as a static class property or something so I can include( InheritTemplate::$actual_template ); within the base template file.)

Is it possible to override that at all? Or is there a better way to accomplish this that I've missed?

gmazzap commented 8 years ago

Hi @chrisvanpatten

sorry for late answer.

Cortex routes support setting templates natively.

When you want to assign a template to a route you can use the 'template' route option, something like:

$routes->addRoute( new QueryRoute(
    '{type:[a-z]+}/latest',
    function(array $matches) {
        return ['post_type'      => $matches['type'], 'posts_per_page' => 5, ];
    },
   [ 'template' => '/path/to/templates/base.php' ]
) );

You can pass "template" without extension,

[ 'template' => '/path/to/templates/base' ]

and .php will be added by Cortex. If your templates use a different extension, you can use the 'cortex.default-template-extension' filter to change it:

add_filter( 'cortex.default-template-extension', function() {
    return 'phtml';
});

another helper is that if you don't pass an absolute path, Cortex will try to load the template from theme (and child theme) folder, e.g. using:

[ 'template' => 'templates/base' ]

means that Cortex will try to load (in order):

Regarding to set the $actual_template based on route, you can use different approaches...

For example, you can assign an ID to your routes:

$routes->addRoute( new QueryRoute(
    '{type:[a-z]+}/latest',
    function(array $matches) {
        return ['post_type'      => $matches['type'], 'posts_per_page' => 5, ];
    },
   [ 'id' => 'latest_articles', 'template' => 'templates/base' ]
) );

after that, you can use 'cortex.matched-after' to set the template, maybe matching the route id:

use Brain\Cortex\Router\MatchingResult;

add_action( 'cortex.matched-after', function( MatchingResult $result ) {

   $routeId = $result->route(); // returns route id
   InheritTemplate::$actual_template = locate_template( "templates/{$routeId}.php" );
} );

doing that, from your base.php template (that is loaded because it was set in 'template' route option) you can use InheritTemplate::$actual_template and obtain what was set inside the callback above.

This is just an example, there are different approaches you can actually take.

chrisvanpatten commented 8 years ago

Iiiinteresting. A bit hackier than I had hoped for, but I imagine that will do the trick. I'll give it a shot and report back. Cheers!

gmazzap commented 8 years ago

If it looks like hackish, you can create a TemplateFinder class that receive a route object and return a template. You can use 'cortex.matched-vars' hook that gives you access to the matched route object:

add_filter( 'cortex.matched-vars', function($vars, RouteInterface $route) {

    $finder = new TemplateFinder();
    $template = $finder->findTemplateForRoute($route);
    if ($template) {
         InheritTemplate::$actual_template = $template;
    }

    return $vars;
}, 10, 2);

Another option is to just use 'template' route option to load the actual template, and inherit the layout to inherit from in the loaded template. Something similar to what happen in Twig templates, where the "layout" is defined in the loaded template, not the other way around.

If you are interested in doing something like this with PHP templates, have a look at Foil ;)

chrisvanpatten commented 8 years ago

Something similar to what happen in Twig templates

That's exactly what I'm aiming for - Twig-like template inheritance, within WordPress. Foil looks like it could help make that a lot easier. Will have to play around with it and report back!

gmazzap commented 8 years ago

If you have questions on Foil, feel free to ask in an issue there... or if you have a stackoverflow account, you can find me in "The Loop" chat http://chat.stackexchange.com/rooms/6/the-loop

chrisvanpatten commented 8 years ago

Got this working with a template engine. Much easier to just ignore WP templating entirely and do it on my own ;-) Thanks!