Comments (4)
Hi @wujekbogdan yep, nothing against that. Don't have much time these days, but if you have time to work on a PR I could review & accept... Or you can wait a bit :)
from cortex.
@wujekbogdan I liked the idea and worked a bit on it.
However, it was no possible to implement it as you described.
Because of compatibilty reasons, the callback must be executed before the "before" callback of the route.
It means that WP_Query
is not parsed yet so won't be possible to do pass a WP_Query
object to the callback.
In next version I plan to improve this, but for now it works like this:
$routes->addRoute( new QueryRoute(
'my/route/{param}',
function ( array $matches ) {
return [
'post_type' => 'my_post_type',
];
},
[
'template' => function ( array $vars, \WP $wp, array $matches ) {
if ( $matches['param'] === 'something') {
return 'a-template.php';
}
return 'another-template.php';
}
]
) );
So you get the query variables that are going to be set, the WP
instance and the matches, but not the query object.
But with a little trick you can obtain your wanted results. First you could define a class like this:
class ConditionalTemplate
{
private $condition;
public function __construct(callable $condition)
{
$this->condition = $condition;
}
public function __invoke(array $vars, \WP $wp, array $matches)
{
add_action('template_redirect', function () use ($matches) {
global $wp_query; // at this point the query is available
$cb = $this->condition;
$template = $cb($matches, $wp_query);
$template and add_filter('template_include', function () use ($template) {
return $template;
}, PHP_INT_MAX);
}, 0);
return null;
}
}
and then:
$routes->addRoute( new QueryRoute(
'my/route/{param}',
function ( array $matches ) {
return [
'post_type' => 'my_post_type',
];
},
[
'template' => new ConditionalTemplate(function (array $matches, \WP_Query $query) {
if ($matches['param'] === 'something' && $query->post_count > 1) {
return '/path/to/a-template.php';
}
return locate_template('index.php');
});
]
) );
This should work quite fine, waiting for easier way on Cortex v2 :)
Note that this feature is not released, you could test by requiring Cortex from branch refactoring-fastorute
I could merge after some feedback.
from cortex.
@wujekbogdan named routes and URL generation were a thing in Cortex 0.* (based on Symphony routing component) and on the Cortex predecessor "Clever Rules" (now abandoned). They were removed from Cortex 1.* switching to FastRoute. But, yes, you can expect they be there on v2.
Besides of that, v2 will use latest version of FastRoute with native support for route groups, some caching mechanism and drop support for PHP 5. ETA: no idea, sometime in 2018, maybe.
from cortex.
I liked the idea and worked a bit on it.
That's great. Thank you very much!
But with a little trick you can obtain your wanted results.
Thanks for the example. It's a bit hacky, but your implementation is quite clean (and quite smart too) anyway.
This should work quite fine, waiting for easier way on Cortex v2 :)
So, you are working on a new version of Cortex? What features can we expect?
Sorry for the offtopic, but I'm very curious what features the new version will bring. For example, I would love to be able not only to define routes but also to easily create URL-s that point to a specific route... but it such change would require named routes (that can possibly break the backward compatibility). Have a look at vue-router named routes and the router.push()
method - this is what I'm talking about.
from cortex.
Related Issues (20)
- Overriding `template_include` hook HOT 6
- 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
- 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.