Skip to content

Commit

Permalink
Merge pull request #5006 from getkirby/release/3.9
Browse files Browse the repository at this point in the history
Fix self-closing snippet with multiple slots
  • Loading branch information
bastianallgeier authored Jan 17, 2023
2 parents 1f41e1e + ff90e6c commit 066b050
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/Template/Slots.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Kirby\Template;

use Countable;

/**
* The slots collection is simplifying
* slot access. Slots can be accessed with
Expand All @@ -14,7 +16,7 @@
* @copyright Bastian Allgeier
* @license https://getkirby.com/license
*/
class Slots
class Slots implements Countable
{
/**
* Creates a new slots collection
Expand All @@ -40,4 +42,12 @@ public function __call(string $name, array $args): Slot|null
{
return $this->__get($name);
}

/**
* Counts the number of defined slots
*/
public function count(): int
{
return count($this->slots);
}
}
15 changes: 11 additions & 4 deletions src/Template/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,22 @@ public function render(array $data = []): string
// take the buffer output from the template as default slot
// and render the snippet as final template output
if (
Snippet::$current !== null &&
Snippet::$current->parent() === $snippet
Snippet::$current === null ||
Snippet::$current->parent() !== $snippet
) {
$template = Snippet::$current->render($data, [
return $template;
}

// no slots have been defined, but the template code
// should be used as default slot
if (Snippet::$current->slots()->count() === 0) {
return Snippet::$current->render($data, [
'default' => $template
]);
}

return $template;
// let the snippet close and render natively
return Snippet::$current->render($data);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/Template/SlotsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ public function testSlots()
$this->assertSame($header, $slots->header());
$this->assertSame($footer, $slots->footer);
$this->assertSame($footer, $slots->footer());
$this->assertCount(2, $slots);
}
}
21 changes: 21 additions & 0 deletions tests/Template/SnippetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ public function testRenderWithoutClosing()
$this->assertSame("<h1>Layout</h1>\ncontent<footer>with other stuff</footer>\n", $snippet->render());
}

/**
* @covers ::render
*/
public function testRenderWithoutClosingAndMultipleSlots()
{
// all output must be captured
$this->expectOutputString('');

$snippet = new Snippet(__DIR__ . '/fixtures/layout-with-multiple-slots.php');

$snippet->slot('header');
echo 'Header content';
$snippet->endslot();

$snippet->slot();
echo 'Body content';
$snippet->endslot();

$this->assertSame("<h1>Layout</h1>\n<header>Header content</header>\n<main>Body content</main>\n", $snippet->render());
}

/**
* @covers ::render
*/
Expand Down
3 changes: 3 additions & 0 deletions tests/Template/fixtures/layout-with-multiple-slots.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Layout</h1>
<header><?= $slots->header() ?></header>
<main><?= $slot ?></main>

0 comments on commit 066b050

Please sign in to comment.