Comments (6)
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 foldertemplates/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.
from cortex.
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!
from cortex.
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 ;)
from cortex.
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!
from cortex.
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
from cortex.
Got this working with a template engine. Much easier to just ignore WP templating entirely and do it on my own ;-) Thanks!
from cortex.
Related Issues (20)
- Sage 9 Integration HOT 6
- Simple example not fired. HOT 1
- About template HOT 2
- [Question] Is there any way to give Cortext routes a higher priority over WP routes? HOT 5
- Preserving variables? HOT 1
- [Question] could you describe how to do a paginated route? HOT 1
- [feature request] Closures as a value for the `template` setting HOT 4
- Render specific post/page/tax archive HOT 1
- How to add body classes depending on a Cortex route - the right way. HOT 2
- Current status for this project? HOT 4
- AJAX requests handling - how to set up CORS? HOT 3
- Routes "players" and "players/" are treated as same url HOT 2
- Handle 404 error HOT 2
- Abandoned packages and Parse error | PHP 8.0.11 HOT 1
- post__in not working HOT 1
- Wordpress 6 Changing on do_parse_request HOT 1
- Issue with wordpress v6 HOT 4
- get_post() does not work
- PHP dependency conflict: "psr/http-message" now requires PHP 7.2 HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cortex.