Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Compiler/SlotCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,17 @@ protected function renderLooseContent(array $children): string
*/
protected function compileSlot(string $name, string $content, string $attributes, string $slotsVariableName): string
{
// Fast path: if content is plain text (no PHP, Blade syntax, or directives),
// use a literal string instead of output buffering.
if (! $this->manager->isFolding() && ! str_contains($content, '<?') && ! str_contains($content, '{{') && ! str_contains($content, '{!!') && ! str_contains($content, '@')) {
Copy link
Copy Markdown
Contributor

@ghabriel25 ghabriel25 Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract as a function for readability? Like

Edited

protected function containsAll(string $haystack, array $needles): bool
{
    return array_reduce($needles, fn($a, $n) => $a && str_contains($haystack, $n), true);
}

protected function containsAny(string $haystack, array $needles): bool
{
    return array_reduce($needles, fn($a, $n) => $a || str_contains($haystack, $n), false);
}

Reference: https://www.php.net/manual/en/function.str-contains.php#128796

$trimmed = trim($content);
$escaped = str_replace(["\\", "'"], ["\\\\", "\\'"], $trimmed);

return '<' . '?php ' . $slotsVariableName . '[\'' . $name . '\'] = new \Illuminate\View\ComponentSlot(\'' . $escaped . '\', ' . $attributes . '); ?>';
}

$contentHandler = $this->manager->isFolding() ? '$__blaze->processPassthroughContent(\'trim\', trim(ob_get_clean()))' : 'trim(ob_get_clean())';

return '<' . '?php ob_start(); ?>'
. $content
. '<' . '?php ' . $slotsVariableName . '[\'' . $name . '\'] = new \Illuminate\View\ComponentSlot(' . $contentHandler . ', ' . $attributes . '); ?>';
Expand Down
15 changes: 15 additions & 0 deletions tests/ComparisonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@
BLADE
));

test('slots with dynamic content', fn () => compare(<<<'BLADE'
<x-card>{{ $title }}</x-card>
BLADE,
['title' => 'Hello World'],
));

test('named slots with dynamic content', fn () => compare(<<<'BLADE'
<x-card>
<x-slot:header>{{ $title }}</x-slot:header>
Body
</x-card>
BLADE,
['title' => 'Hello World'],
));

test('deeply nested same component with different components interleaved', fn () => compare(<<<'BLADE'
<x-card class="outer">
<x-wrapper>
Expand Down
6 changes: 3 additions & 3 deletions tests/Compiler/CompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
'<?php $__attrs'. $hash .' = [\'class\' => \'mt-8\']; ?> ',
'<?php $__slots'. $hash .' = []; ?> ',
'<?php $__blaze->pushData($__attrs'. $hash .'); ?> ',
'<?php ob_start(); ?> Body <?php $__slots'. $hash .'[\'slot\'] = new \Illuminate\View\ComponentSlot(trim(ob_get_clean()), []); ?> ',
'<?php ob_start(); ?> Header <?php $__slots'. $hash .'[\'header\'] = new \Illuminate\View\ComponentSlot(trim(ob_get_clean()), [\'class\' => \'p-2\']); ?> ',
'<?php ob_start(); ?> Footer <?php $__slots'. $hash .'[\'footer\'] = new \Illuminate\View\ComponentSlot(trim(ob_get_clean()), [\'class\' => \'mt-4\']); ?> ',
'<?php $__slots'. $hash .'[\'slot\'] = new \Illuminate\View\ComponentSlot(\'Body\', []); ?> ',
'<?php $__slots'. $hash .'[\'header\'] = new \Illuminate\View\ComponentSlot(\'Header\', [\'class\' => \'p-2\']); ?> ',
'<?php $__slots'. $hash .'[\'footer\'] = new \Illuminate\View\ComponentSlot(\'Footer\', [\'class\' => \'mt-4\']); ?> ',
'<?php $__blaze->pushSlots($__slots'. $hash .'); ?> ',
'<?php _'. $hash .'($__blaze, $__attrs'. $hash .', $__slots'. $hash .', [], [], $__this ?? (isset($this) ? $this : null)); ?> ',
'<?php if (! empty($__slotsStack'. $hash .')) { $__slots'. $hash .' = array_pop($__slotsStack'. $hash .'); } ?> ',
Expand Down
Loading