How to render a blade template from filter function

blade
sage9

#1

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.


Manually render blade template in functions.php
#2

@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}'); ?>";
    });

#3

Wow it was simply the template() function that I needed. Thank you sir.

For all the ajax-load-more peeps. If you’ve going for the Theme Repeater add-on, in Sage 9 this is what works:

  1. Create the resources/views/alm_templates directory.
  2. Add your default.php or whatever.php repeater file in there, with something like this inside (rocking the template() function, of course):
# resources/views/alm_templates/default.php
<?php echo template('partials.content-' . get_post_type()); ?>