diff --git a/src/ComponentNode.php b/src/ComponentNode.php index a4c8918..609ef61 100644 --- a/src/ComponentNode.php +++ b/src/ComponentNode.php @@ -30,7 +30,8 @@ public function compile(Compiler $compiler): void $compiler ->write(sprintf("if ($%s) {\n", $template)) - ->write('$oldSlots = $slots ?? [];' . PHP_EOL) + ->write('$slotsStack = $slotsStack ?? [];' . PHP_EOL) + ->write('$slotsStack[] = $slots ?? [];' . PHP_EOL) ->write('$slots = [];' . PHP_EOL) ->write("ob_start();" . PHP_EOL) ->subcompile($this->getNode('slot')) @@ -42,7 +43,7 @@ public function compile(Compiler $compiler): void $compiler ->raw(");\n") - ->write('$slots = $oldSlots;' . PHP_EOL) + ->write('$slots = array_pop($slotsStack);' . PHP_EOL) ->write("}\n") ; } diff --git a/tests/ComponentTest.php b/tests/ComponentTest.php index d6d9c3e..6b89b28 100644 --- a/tests/ComponentTest.php +++ b/tests/ComponentTest.php @@ -7,6 +7,8 @@ class ComponentTest extends TestCase { + protected $twig; + protected function setupTwig(): \Twig\Environment { $loader = new \Twig\Loader\FilesystemLoader(__DIR__ . '/templates'); @@ -20,12 +22,15 @@ protected function setupTwig(): \Twig\Environment return $twig; } + public function setUp(): void + { + $this->twig = $this->setupTwig(); + } + /** @test */ public function render_simple_component() { - $twig = $this->setupTwig(); - - $html = $twig->render('test_simple_component.twig'); + $html = $this->twig->render('test_simple_component.twig'); $this->assertEquals(<< test @@ -35,9 +40,7 @@ public function render_simple_component() /** @test */ public function render_simple_component_with_dash() { - $twig = $this->setupTwig(); - - $html = $twig->render('test_simple_component_with_dash.twig'); + $html = $this->twig->render('test_simple_component_with_dash.twig'); $this->assertEquals(<< test @@ -47,9 +50,7 @@ public function render_simple_component_with_dash() /** @test */ public function render_simple_component_in_folder() { - $twig = $this->setupTwig(); - - $html = $twig->render('test_simple_component_in_folder.twig'); + $html = $this->twig->render('test_simple_component_in_folder.twig'); $this->assertEquals(<< test @@ -59,9 +60,7 @@ public function render_simple_component_in_folder() /** @test */ public function render_component_with_slots() { - $twig = $this->setupTwig(); - - $html = $twig->render('test_with_slots.twig'); + $html = $this->twig->render('test_with_slots.twig'); $this->assertEquals(<<test
test
@@ -71,9 +70,7 @@ public function render_component_with_slots() /** @test */ public function render_xtags_with_slots() { - $twig = $this->setupTwig(); - - $html = $twig->render('test_xtags_with_slots.twig'); + $html = $this->twig->render('test_xtags_with_slots.twig'); $this->assertEquals(<<test
test
@@ -83,9 +80,7 @@ public function render_xtags_with_slots() /** @test */ public function render_nested_xtags_with_slots() { - $twig = $this->setupTwig(); - - $html = $twig->render('test_nested_xtags_with_slots.twig'); + $html = $this->twig->render('test_nested_xtags_with_slots.twig'); $this->assertEquals(<<[outer name]
[inner name][inner slot]
@@ -93,11 +88,20 @@ public function render_nested_xtags_with_slots() } /** @test */ - public function render_component_with_xtags() + public function render_deeply_nested_xtags_with_slots() { - $twig = $this->setupTwig(); + $html = $this->twig->render('test_deeply_nested_xtags_with_slots.twig'); + $html = preg_replace('/\s{2,}/', '', $html); // ignore whitespace difference - $html = $twig->render('test_xtags_component.twig'); + $this->assertEquals(<<A
BC
D
FG
+ HTML, $html); + } + + /** @test */ + public function render_component_with_xtags() + { + $html = $this->twig->render('test_xtags_component.twig'); $this->assertEquals(<< test1 @@ -109,9 +113,7 @@ public function render_component_with_xtags() /** @test */ public function render_component_with_attributes() { - $twig = $this->setupTwig(); - - $html = $twig->render('test_with_attributes.twig'); + $html = $this->twig->render('test_with_attributes.twig'); $this->assertEquals(<< diff --git a/tests/templates/test_deeply_nested_xtags_with_slots.twig b/tests/templates/test_deeply_nested_xtags_with_slots.twig new file mode 100644 index 0000000..e8a8714 --- /dev/null +++ b/tests/templates/test_deeply_nested_xtags_with_slots.twig @@ -0,0 +1,15 @@ + + A + + B + C + + E + D + + F + G + + + + \ No newline at end of file