I am trying to write a custom directive in laravel. However, it only returns the path of my blade partial as a string, not the actual html like @include does.
@customInclude('authenticated/partials/header2')
Blade::directive('customInclude', function($partial){
if(Config::get('constants.ORG_ID') === 'organizationId'){
return "<?php echo $partial; ?>";
}
});
I want the custom directive to return the html found in the path 'authenticated/partials/header2' , however, it seems that blade is not recognizing that the path is a path in my php. My custom directive lives in the AppServiceProvider.php file btw. Does anyone know how @include works really well so they can explain why my path isn't being recognized.
@include is just like a basic PHP include, it includes a "partial" view into your view. @extends lets you "extend" a template, which defines its own sections etc. A template that you can extend will define its own sections using @yield , which you can then put your own stuff into in your view file.
Laravel blade lets you extend views when you define a child page in the framework. It provides a clear directive that specifies which child page should you put up. Views that extend a blade layout tend to inject the content into the layout section using the different section directives.
In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.
Two of the primary benefits of using Blade are template inheritance and sections. We can define a blade page as a combination of layout and sections. Since most of the general web applications will have the same layout across the web pages.
Cool question, it took a bit of digging, but you can replicate what laravel does quite easily:
Blade::directive('customInclude', function($partial){
if(Config::get('constants.ORG_ID') === 'organizationId'){
return "<?php echo view($partial); ?>";
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With