Skip to content

Commit

Permalink
Merge pull request #5 from Stillat/default-slot-value
Browse files Browse the repository at this point in the history
Make `$slot` value always available for developer happiness
  • Loading branch information
JohnathonKoster authored Jan 21, 2025
2 parents 176fb6d + 17503e4 commit 5cae04c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Compiler/TemplateCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ protected function compileNodes(array $nodes): string
if ($__slotContainerVarSuffix->hasDefaultSlotContent()) { $__componentData['slot'] = $__slotContainerVarSuffix->getDefaultContent(); }
(function () use ($__tmpVars, $__componentData) {
$slot = \Stillat\Dagger\Runtime\SlotContainer::getEmptySlot();
extract($__tmpVars);extract($componentVarName->getMacros());extract($__componentData);extract($componentVarName->data->toArray());
?>#inner#<?php
})();
Expand Down
11 changes: 11 additions & 0 deletions src/Runtime/SlotContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ class SlotContainer
{
const DEFAULT_SLOT_KEY = '-default-';

protected static ?ComponentSlot $emptySlot = null;

protected array $contents = [];

public static function getEmptySlot(): ComponentSlot
{
if (static::$emptySlot === null) {
static::$emptySlot = new ComponentSlot;
}

return static::$emptySlot;
}

public function setSlotContent(string $slotName, string $contents, array $attributes = []): void
{
$this->contents[$slotName] = new ComponentSlot($contents, $attributes);
Expand Down
4 changes: 4 additions & 0 deletions tests/Compiler/SlotsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,7 @@
$this->render($template)
);
});

test('default slot value is injected', function () {
$this->assertSame('<div></div>', $this->render('<c-empty_slot />'));
});
1 change: 1 addition & 0 deletions tests/resources/components/empty_slot.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>{{ $slot }}</div>

0 comments on commit 5cae04c

Please sign in to comment.