-
Notifications
You must be signed in to change notification settings - Fork 20
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
Comments
sorry for late answer. Cortex routes support setting templates natively. When you want to assign a template to a route you can use the $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,
and 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:
means that Cortex will try to load (in order):
Regarding to set the 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 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 This is just an example, there are different approaches you can actually take. |
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! |
If it looks like hackish, you can create a 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 If you are interested in doing something like this with PHP templates, have a look at Foil ;) |
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! |
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 |
Got this working with a template engine. Much easier to just ignore WP templating entirely and do it on my own ;-) Thanks! |
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 caninclude( 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?
The text was updated successfully, but these errors were encountered: