Skip to content

Commit c4f72aa

Browse files
committed
Added RenderContext::getContent() + More tests
1 parent f34071c commit c4f72aa

File tree

9 files changed

+78
-7
lines changed

9 files changed

+78
-7
lines changed

src/RenderContext.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ class RenderContext
3434

3535
private array $parentData = [];
3636

37+
private string $childContent;
38+
3739
public function __construct(
3840
callable $fetchTpl,
3941
array $data = [],
40-
?Sections $sections = null
42+
?Sections $sections = null,
43+
string $childContent = ''
4144
) {
4245
$this->fetchTpl = $fetchTpl;
4346
$this->data = $data;
4447
$this->sections = $sections;
48+
$this->childContent = $childContent;
4549
}
4650

4751
public function parent(string $name, array $data = []): void
@@ -91,6 +95,11 @@ public function section(string $name, ?string $default = null): ?string
9195
return $this->sections->get($name) ?? $default;
9296
}
9397

98+
public function getContent(): string
99+
{
100+
return $this->childContent;
101+
}
102+
94103
public function getData(): array
95104
{
96105
return $this->data;

src/Template.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ class Template
3636
/** @var string */
3737
private const NAME_SEPARATOR = '::';
3838

39-
/** @var string */
40-
private const CONTENT_SECTION_KEY = 'content';
41-
4239
protected Renderer $engine;
4340

4441
protected string $filePath;
@@ -47,6 +44,8 @@ class Template
4744

4845
private Sections $sections;
4946

47+
private string $childContent = '';
48+
5049
public function __construct(
5150
string $name,
5251
Renderer $engine,
@@ -75,7 +74,12 @@ public function render(array $data = []): string
7574
$this->withData($data);
7675
$data = $this->data;
7776
$file = $this->getPath();
78-
$context = new RenderContext([$this, 'fetch'], $data, $this->sections);
77+
$context = new RenderContext(
78+
[$this, 'fetch'],
79+
$data,
80+
$this->sections,
81+
$this->childContent
82+
);
7983
$content = $this->buffer((
8084
fn () => extract($data, EXTR_SKIP) & include $file
8185
)->bindTo($context));
@@ -84,7 +88,7 @@ public function render(array $data = []): string
8488

8589
if (! empty($parentTpl)) {
8690
$parent = $this->engine->createTemplate($parentTpl, $this->sections);
87-
$parent->getSections()->add(self::CONTENT_SECTION_KEY, $content);
91+
$parent->setChildContent($content);
8892
$content = $parent->render($context->getParentData());
8993
}
9094

@@ -137,6 +141,13 @@ public function getSections(): Sections
137141
return $this->sections;
138142
}
139143

144+
public function setChildContent(string $content): self
145+
{
146+
$this->childContent = $content;
147+
148+
return $this;
149+
}
150+
140151
private function setFilePathFromName(string $name): self
141152
{
142153
$chunks = explode(self::NAME_SEPARATOR, $name);
@@ -160,6 +171,13 @@ private function setFilePathFromName(string $name): self
160171
return $this;
161172
}
162173

174+
/**
175+
* @param callable $wrap
176+
*
177+
* @return false|string
178+
*
179+
* @throws Throwable
180+
*/
163181
private function buffer(callable $wrap)
164182
{
165183
$level = ob_get_level();

test/Asset/nested-0.tpl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<h1>Parent:</h1>
2+
<ul>
3+
<?= $this->getContent() ?>
4+
</ul>

test/Asset/nested-1.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php $this->parent('assets::nested-0', $this->getData()); ?>
2+
<li>#1;</li>
3+
<?= $this->getContent() ?>

test/Asset/nested-2.tpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php $this->parent('assets::nested-1', $this->getData()); ?>
2+
<li>#2;</li>
3+
<?= $this->getContent() ?>

test/Asset/nested-3.tpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?php $this->parent('assets::nested-2', $this->getData()); ?>
2+
<li>#3;</li>

test/Asset/parent.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<h1>Parent</h1>
22

3-
<?= $this->section('content') ?>
3+
<?= $this->getContent() ?>
44

55
<p><?= $foo; ?></p>

test/RenderContextTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ public function testFetch(): void
139139
$this->assertSame('hello world', $output);
140140
}
141141

142+
public function testGetContent(): void
143+
{
144+
$callable = fn () => null;
145+
$childContent = '<p>test</p>';
146+
$context = new RenderContext($callable, [], null, $childContent);
147+
148+
$this->assertSame($childContent, $context->getContent());
149+
}
150+
142151
public function testApplyThrowsErrorWhenFunctionNotFound(): void
143152
{
144153
$this->expectException(RuntimeException::class);

test/TemplateTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ public function testCanLoadParentFromChild(): void
158158
$this->assertSame($expected, $output);
159159
}
160160

161+
/**
162+
* @throws Throwable
163+
*/
164+
public function testCanLoadNestedTemplates(): void
165+
{
166+
$renderer = new Renderer(['assets' => self::ASSETS_DIR]);
167+
168+
$tpl = new Template('assets::nested-3', $renderer);
169+
170+
$output = $tpl->render();
171+
172+
$expected =<<<EXP
173+
<h1>Parent:</h1>
174+
<ul>
175+
<li>#1;</li>
176+
<li>#2;</li>
177+
<li>#3;</li>
178+
</ul>
179+
EXP;
180+
181+
$this->assertSame($expected, $output);
182+
}
183+
161184
/**
162185
* @throws Throwable
163186
*/

0 commit comments

Comments
 (0)