Reputation: 9749
I have a custom Blade directive from which I'm trying to include a partial with the Blade syntax @include()
. The problem is that I have a custom views namespace:
\Blade::directive('name', function() {
$viewsNamespace = 'viewsNameSpace::';
$formPartial = $viewsNamespace . 'partials._form';
return "{{ @include({$formPartial}) }}";
});
This outputs the error,
Class 'viewsNameSpace' not found
because its interpreting viewsNameSpace::
as a class.
This outputs just the string without parsing it:
return "@include('{$formPartial}')";
And this is not throwing any errors but its not loading the partial:
return "{{ @include('{$formPartial}') }}";
Please note that the partial is working when I'm using in in a template like this:
@include('viewsNameSpace::partials._form')
but I can't make it work from the directive.
Any help and suggestions would be appreciated! Thank you!
Upvotes: 11
Views: 3659
Reputation: 4202
If anyone comes across this question since its post in '16, Laravel now has a blade helper to create a custom include
directive (since version 5.6.5):
Blade::include('viewsNameSpace::path.to.view.file');
This is to be added to the boot()
method of your app service provider. More examples here.
Upvotes: 2
Reputation: 2708
Try this :
return "<?php echo view('viewsNameSpace::partials._form')->render(); ?>"
;
Upvotes: 0
Reputation: 9749
This is how I made it work:
return "<?php echo view('$formPartial')->render(); ?>";
Where $formPartial
is 'viewsNameSpace::partials._form'
.
Upvotes: 8