How to render a blade template from filter function

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.

1 Like

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

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()); ?>
2 Likes

Hello @ pascallaliberte, @ Webstractions

I am trying to implement selective refresh on a custotmizer setting. The selective refresh callback should render the header partial.

I have had some success with

'render_callback'     => function() {
    echo \App\template('partials.header');
},

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

Thank you