Loading plugin views. Add a location to look for blades?

I ask a lot of annoying questions on this forum.

When I build feature plugins for my sites I like to include views with them so I’m not creating too many interdependencies between my theme and plugins.

In the past I’ve used Gamajo Template Loader to let the plugin keep its own views, and let the theme override them, like WooCommerce and other plugins do.

But this method doesn’t work with Blade templates because Sage’s blade functionality isn’t called when Gamajo does its magic.

A solution to this would be to allow Sage’s Blade implementation to accept a path to look for views in the plugin directory. It would check the theme first for an override, and fall back to the plugin path.

It would be easy enough to check for Blade functionality in the plugin before including a view, and besides that I’m much more comfortable having a plugin require Sage9 (any Sage9) than I am requiring specific template files within that Sage9 theme.

So what do you think? Is this possible now? Is it a good feature suggestion if not?

Thanks for reading.

1 Like

Did you see this section?

I have seen that, and I want to do sort of the reverse. I want the plugin to tell the theme about its views, rather than the other way around.

I guess I could wrap that in a filter and use that, though.

I’ll see if that works. Thanks!

2 Likes

@MWDelaney - Could you share an example of what that might look like? I’m creating some functionality and would like to load a blade template from a specific directory that is not the standard view directory.

Using the code below works great to render a template from the standard directory, but how to I have it look in a custom directory? Ideally allowing for someone to override these in their own theme directory.

$template = 'plugin-views.template.blade.php';
echo \App\template($template);

So I’m just proceeding at the moment with the standard resources/views directory and have found something odd. There’s probably a very simple solution, but I’ve googled and have had no luck.

Basically, I can render an existing template. say

    $template = 'partials.content-events.blade.php';
    echo \App\template($template);

this works fine. however, if I add a new template to the partials directory it says it cannot find the template. I’ve cleared the cache as well. seems very strange as I can include the new partial directly from another template via @include

Any ideas?

Ok. Error on my part by blindly copy and pasting something.

The template names do not need ‘.blade.php’. They only need the dot ‘.’ separated path/filename.

So for the example

$template = 'plugin-views.template';
echo \App\template($template);

I’m really uncertain why the one rendered though.

1 Like

I’m looking for a solution to include a Blade view in a Shortcode from a Plugin. The only way I find is to include the plugin path in view.php (thanks @kalenjohnson). Any ideas?

You’d have to be absolutely sure that the theme was Sage-based, but you should be able to do something like this:

// Set up the template file we want to include
$template = plugin_dir_path(__FILE__) . "views/shortcode";

// Set up template data if/as necessary
$data = apply_filters('sage/template/app-data/data', []);

// return the populated template using Sage’s built-in template() function
return \App\template($template, $data);

This almost definitely won’t work out of the box (I’m typing this from a phone) but it might get you moving in the right direction. Hopefully in the future Clover will help ease some of these difficulties.

In the context of clover, I’m doing this:
In PluginNameServiceProvider.php:

public function boot()
   {
     if ( function_exists( '\Roots\view' ) ) {
       $view = \Roots\view();

       $view->addNamespace('PluginName', __DIR__ . '/../../resources/views/');
     }
   }

And then within a route service provider or wherever you want to render the view:

return \Roots\view('PluginName::[template name in resources/views]', [DATA ARRAY])->render();

I havent yet figured out how to attach a controller / View composer yet but will post back when I do but any data passed to the DATA ARRAY can be rendered with {{ $data }} as expected

1 Like

I should add this is in the context of Acorn/Sage 10 , not sage 9 as I see this thread is marked

Figured out how to attach the composer controller…
in the boot() call above, I just added…

Facades\View::composer('PluginName::app', App::class);

(and use Illuminate\Support\Facades; at the top)

1 Like