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

Overriding template_include hook #14

Closed
chrisvanpatten opened this issue Jul 4, 2016 · 6 comments
Closed

Overriding template_include hook #14

chrisvanpatten opened this issue Jul 4, 2016 · 6 comments
Assignees
Labels

Comments

@chrisvanpatten
Copy link

chrisvanpatten commented Jul 4, 2016

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 gmazzap self-assigned this Jul 6, 2016
@gmazzap
Copy link
Collaborator

gmazzap commented Jul 6, 2016

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):

  • templates/base.php relative to child theme folder
  • templates/base.php relative to theme folder

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
Copy link
Author

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
Copy link
Collaborator

gmazzap commented Jul 6, 2016

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
Copy link
Author

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
Copy link
Collaborator

gmazzap commented Jul 6, 2016

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
Copy link
Author

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

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

No branches or pull requests

2 participants