Code Monkey home page Code Monkey logo

Comments (6)

gmazzap avatar gmazzap commented on September 15, 2024

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.

from cortex.

chrisvanpatten avatar chrisvanpatten commented on September 15, 2024

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.

gmazzap avatar gmazzap commented on September 15, 2024

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.

chrisvanpatten avatar chrisvanpatten commented on September 15, 2024

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.

gmazzap avatar gmazzap commented on September 15, 2024

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.

chrisvanpatten avatar chrisvanpatten commented on September 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.