Comments (2)
Hi @vittore
Cortex is just a routing library, how to render is actually up to you :)
If you use a QueryRoute
like in your example, by default Cortex will let WordPress to choose template, which in your case ends up in loading index.php
.
If you use the 'template'
option of Cortex to force a specific template. E.g.
$routes->addRoute( new Route\QueryRoute(...), [ 'template' => 'my-template.php' ] );
Will make WordPress to load 'my-template.php'
in theme or child theme.
This still leave you the isue how to pass variables... The WordPress way is to use globals
add_action('cortex.routes', function (Route\RouteCollectionInterface $routes) {
$routes->addRoute(new Route\QueryRoute(
function (array $matches) {
$myObj = new myObj();
global $my_html_string;
$my_html_string = $myObj->getHtml($matches['code']);
}
}
});
and then in index.php
:
global $my_html_string;
echo $my_html_string;
Another way would be to use WordPress filters:
add_action('cortex.routes', function (Route\RouteCollectionInterface $routes) {
$routes->addRoute(new Route\QueryRoute(
function (array $matches) {
add_filter('my_custom_html', function() {
$myObj = new myObj();
return $myObj->getHtml($matches['code']);
});
}
}
});
and then in index.php
:
echo apply_filters('my_custom_html', '');
If you want to use something more "structured" you should setup something on your own... maybe use a template engine...
Cortex is a routing library, which means that allows you to set query variables from URL, maybe doing something custom when the route matches, but how to render template is out of its scope.
from cortex.
Thanks for your support.
First solution (use of a global var into template) is my solution. Not best and clean, but it works perfectly.
I close this issue.
v.
from cortex.
Related Issues (20)
- Overriding `template_include` hook HOT 6
- Sage 9 Integration HOT 6
- Simple example not fired. HOT 1
- [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.