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?
@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.
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.
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
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