Skip to content

Commit

Permalink
dynamic slots (#33724)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbl authored Aug 1, 2020
1 parent 40c2121 commit 8f70e82
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Illuminate/View/Compilers/ComponentTagCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,14 @@ protected function compileClosingTags(string $value)
*/
public function compileSlots(string $value)
{
$value = preg_replace_callback('/<\s*x[\-\:]slot\s+name=(?<name>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+))\s*>/', function ($matches) {
return " @slot('".$this->stripQuotes($matches['name'])."') ";
$value = preg_replace_callback('/<\s*x[\-\:]slot\s+(:?)name=(?<name>(\"[^\"]+\"|\\\'[^\\\']+\\\'|[^\s>]+))\s*>/', function ($matches) {
$name = $this->stripQuotes($matches['name']);

if ($matches[1] !== ':') {
$name = "'{$name}'";
}

return " @slot({$name}) ";
}, $value);

return preg_replace('/<\/\s*x[\-\:]slot[^>]*>/', ' @endslot', $value);
Expand Down
8 changes: 8 additions & 0 deletions tests/View/Blade/BladeComponentTagCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public function testSlotsCanBeCompiled()
$this->assertSame("@slot('foo') \n".' @endslot', trim($result));
}

public function testDynamicSlotsCanBeCompiled()
{
$result = $this->compiler()->compileSlots('<x-slot :name="$foo">
</x-slot>');

$this->assertSame("@slot(\$foo) \n".' @endslot', trim($result));
}

public function testBasicComponentParsing()
{
$this->mockViewFactory();
Expand Down

0 comments on commit 8f70e82

Please sign in to comment.