diff --git a/README.md b/README.md index e2913c5..1b3db98 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,26 @@ The `@return` blade directive simply stops any further processing of the current @return ``` +### @slotdefault + +When you've an optional slot this directive gives you a cleaner way of defining a fallback. Normally you do something like this: + +```blade +@if ($slot->isEmpty()) + This is default content if the slot is empty. +@else + {{ $slot }} +@endif +``` + +#### Usage + +```blade +@slotdefault('slot') + This is default content if the slot is empty. +@endslotdefault +``` + ### @slots The `@slots` blade directive is used within blade components. diff --git a/src/BladeDirectivesServiceProvider.php b/src/BladeDirectivesServiceProvider.php index 8ecec60..c8e8e8c 100644 --- a/src/BladeDirectivesServiceProvider.php +++ b/src/BladeDirectivesServiceProvider.php @@ -53,6 +53,15 @@ public function register() exceptProps($expression); ?>"; }); + + Blade::directive('slotdefault', function ($expression) { + $slot = trim($expression, '\'"'); + return "isNotEmpty()): echo $$slot; else: ?>"; + }); + + Blade::directive('endslotdefault', function ($expression) { + return ''; + }); } public function boot()