Skip to content

Instantly share code, notes, and snippets.

@userabuser
Last active May 2, 2021 13:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save userabuser/7134a6627a2bf8127bdbd56a7759eb2d to your computer and use it in GitHub Desktop.
Save userabuser/7134a6627a2bf8127bdbd56a7759eb2d to your computer and use it in GitHub Desktop.
Custom Laravel Blade Directive for use with @yield and @section (Laravel)
<?php
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider {
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
/**
* Create a custom directive that can be called by @yield('directivename')
*
* Example:
*
* In your parent template (parent.blade.php):
*
* <div>@yield('customdirective')</div>
*
* In your child blade template (child.blade.php):
*
* @customdirective('some value)
*
* Result:
*
* <div>some value</div>
*/
Blade::directive('customdirective', function($expression) {
$render = "<?php \$__env->startSection('customdirective'); ?>";
$render .= "<?php echo {$expression}; ?>";
$render .= "<?php \$__env->stopSection(); ?>";
return $render;
});
}
//etc...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment