Skip to content

Commit

Permalink
Slotdefault directive (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin authored Mar 19, 2024
1 parent 2d2fb62 commit a507a60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/BladeDirectivesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ public function register()
<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag; ?>
<?php \$attributes = \$attributes->exceptProps($expression); ?>";
});

Blade::directive('slotdefault', function ($expression) {
$slot = trim($expression, '\'"');
return "<?php if(isset($$slot) && ($$slot)->isNotEmpty()): echo $$slot; else: ?>";
});

Blade::directive('endslotdefault', function ($expression) {
return '<?php endif; ?>';
});
}

public function boot()
Expand Down

0 comments on commit a507a60

Please sign in to comment.