Brain-WP / Cortex

Routing system for WordPress
MIT License
348 stars 20 forks source link

Sage 9 Integration #15

Closed jgeschwendt closed 8 years ago

jgeschwendt commented 8 years ago

I'm trying to use Cortex with the Sage 9 theme. This seems like another issue with overriding the template_include action. Is there a possible workaround to get this to work with the new template structure?

jgeschwendt commented 8 years ago

I added

/**
 * Use theme wrapper passing through Brain/Cortex
 */
add_filter( 'cortex.matched-vars', function($vars, RouteInterface $route) {
    $route['template'] = ((new Template(new Wrapper($route['template'])))->layout());
    return $vars;
}, 10, 2);

to the sage functions.php.

This seems to be working. Are there any foreseeable issues with this solution?

gmazzap commented 8 years ago

This seems to be working. Are there any foreseeable issues with this solution?

No, I will not breack BC for it. However, you may find easier / cleaner to write a SageRoute that set's the template.

The Brain\Cortex\Route\DerivativeRouteTrait makes the task very easy.

The class should be something like:

namespace YourTheme;

use Brain\Cortex\Route\RouteInterface;
use Brain\Cortex\Route\QueryRoute;
use Brain\Cortex\Route\DerivativeRouteTrait;
use Roots\Sage\Template;
use Roots\Sage\Template\Wrapper;

class SageRoute implements RouteInterface {

   use DerivativeRouteTrait;

   public function __construct(RouteInterface $route, $tmpl) {
       $before = $route['before'];
       $route['before'] = function($vars, $wp, &$template, $matches) use($before, $tmpl) {
          // run route "before" callback if there was one
          is_callable($before) and $before($vars, $wp, $template, $matches);
          // edit template thanks to pass-by-reference
          $template =  (new Template(new Wrapper($tmpl)))->layout();
       };

       $this->route = $route;
   }
}

With this class, the code to add routes becomes something like:

add_action('cortex.routes', function(Routes $routes) {

    $route = new SageRoute(new QueryRoute('{type:[a-z]+}/latest', function(array $matches) {
          return ['post_type' => $matches['type'], 'posts_per_page' => 5];
    }), 'some-template-name');

    $routes->addRoute($route);

});

Not tested at all, but should work.

wujekbogdan commented 6 years ago

Thanks @gmazzap for the code snippet.

Here's a solution for Sage8.

    public function __construct(RouteInterface $route, $templateFile)
    {
        $before = $route['before'];
        $route['before'] = function ($vars, $wp, &$template, $matches) use ($before, $templateFile) {
            is_callable($before) and $before($vars, $wp, $template, $matches);
            $template = Wrapper\SageWrapping::wrap($templateFile);
        };

        $this->route = $route;
    }
jrgd commented 6 years ago

the code @gmazzap kindly provided seems to break in the current Sage9 when I add it to functions.php or in the /app; anyone experiencing something similar with the latest Sage9?

It chokes on (new Template(new Wrapper( as it seems Template and TemplateWrapper have been removed since then.

zacarias-wichipu-com commented 6 years ago

@jrgd Did you find any solution? I'm with the same problem as you :(

gmazzap commented 6 years ago

I have no experience with Sage, maybe try to open an issue there, feel free to pen an issue here if something in Cortex that can be fixed/improved.