I’m looking to render a blade template from within a filter function. Anybody got any tips?
Overall idea:
Catch a route/rewrite_rule like this one /partial/archive/<post_type>(/<pagenum>/)
In a filter function, I’d catch the route, and then echo back the right blade template e.g. partial-archive-<post_type>
The context:
We’re building a sage 9 project, we’d like scroll-loading (fetching additional pages and appending the post previews to the page) but our custom post type archive urls are used by custom-design pages that are not archives, with a custom list of recent posts at the bottom (ACF flex module) to which we’d like to add scroll-loading.
We tried ajax-load-more with theme repeaters, but to make their alm_templates templates pull from blade, I’m right back at the same blade-rendering-outside-of-blade problem.
@pascallaliberte Maybe this answer to an infinite-scroll topic might help?
Another route is to use the @inject directive to inject your service into the Blade view. This directive does not work for Sage9 because the directive looks for the app() helper from Laravel. You will need to overwrite it via setup.php like this:
/**
* Overwrite @inject() Blade directive
* IMPORTANT: This needs to follow the binding of the Blade Provider above.
*/
sage('blade')->compiler()->directive('inject', function($expression) {
$segments = explode(',', preg_replace("/[\(\)\\\"\']/", '', $expression));
$variable = trim($segments[0]);
$service = trim($segments[1]);
return "<?php \${$variable} = App\sage('{$service}'); ?>";
});
The partial is rendered, however, the variables passed by the controller are not available to the partial in this way. I have worked around it by changing the controller methods for static methods, so in the partial I call the method instead of accessing the variable. Eg:
In the controller, I changed
public static function primaryNavTextColor() {
return get_theme_mod( 'primary_nav_text_color' );
}
to
public function primaryNavTextColor() {
return get_theme_mod( 'primary_nav_text_color' );
}
And in the partial, I changed
{{ $primary_nav_text_color }}
for
{{ App::primaryNavBackgroundColor() }}
But I wonder if there is a way to access the controller variables available without having to change the controller methods to static