# How to render a blade template from filter function

**URL:** https://discourse.roots.io/t/how-to-render-a-blade-template-from-filter-function/10877
**Category:** sage
**Tags:** sage9, blade
**Created:** 2017-11-10T22:12:41Z
**Posts:** 4

## Post 1 by @pascallaliberte — 2017-11-10T22:12:42Z

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.

---

## Post 2 by @Webstractions — 2017-11-10T22:40:30Z

@pascallaliberte Maybe this answer to an infinite-scroll topic might help?

> [@Roots Sage with Jetpack Infinite Scroll](https://discourse.roots.io/t/roots-sage-with-jetpack-infinite-scroll/10007/2):
>
> This is how I got it working on one recent site. I had to use the “render” callback function to iterate through the loop and use sage (9) template function. I hope it helps you figure it out! // Add support for Jetpack's Infinite Scroll add\_theme\_support('infinite-scroll', ['container' =\> 'main', 'footer' =\> false, 'type' =\> 'scroll', 'wrapper' =\> false, 'posts\_per\_page' =\> 12, 'render' =\> \_\_NAMESPACE\_\_ . '\\infinite\_scroll',]); …

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

---

## Post 3 by @pascallaliberte — 2017-11-10T23:33:46Z

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

---

## Post 4 by @a.santos — 2020-05-16T08:02:02Z

Hello @ [pascallaliberte](https://discourse.roots.io/t/how-to-render-a-blade-template-from-filter-function/10877/3), @ [Webstractions](https://discourse.roots.io/u/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
