Skip to content

Commit 358495f

Browse files
authored
Merge pull request #39 from michalsn/fix/view_fragment
fix: view fragment
2 parents b8b926b + ef10713 commit 358495f

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/View/View.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,6 @@ public function render(string $view, ?array $options = null, ?bool $saveData = n
158158
*/
159159
public function fragment(string $name)
160160
{
161-
if (empty($this->renderVars['options']['fragments'])) {
162-
return;
163-
}
164-
165161
$this->fragmentStack[] = $name;
166162

167163
ob_start();
@@ -174,10 +170,6 @@ public function fragment(string $name)
174170
*/
175171
public function endFragment()
176172
{
177-
if (empty($this->renderVars['options']['fragments'])) {
178-
return;
179-
}
180-
181173
$contents = ob_get_clean();
182174

183175
if ($this->fragmentStack === []) {
@@ -217,7 +209,7 @@ protected function renderFragment(string $fragmentName)
217209
*/
218210
public function include(string $view, ?array $options = null, $saveData = true): string
219211
{
220-
if ($this->fragmentStack !== []) {
212+
if ($this->fragmentStack !== [] && ! empty($this->renderVars['options']['fragments'])) {
221213
$options['fragments'] = $this->renderVars['options']['fragments'];
222214
echo $this->render($view, $options, $saveData);
223215

tests/CommonTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,28 @@ public function testViewFragmentSaveData(): void
6363

6464
$this->assertSame($expected, view_fragment('view_fragment', ['sample1', 'sample2'], $data, ['saveData' => true]));
6565
}
66+
67+
public function testViewFragmentWithLayout(): void
68+
{
69+
$data = [
70+
'testString1' => 'Hello World',
71+
'testString2' => 'Hello World',
72+
];
73+
74+
$expected = "Hello World, fragment1!\nHello World, fragment2!\n";
75+
76+
$this->assertSame($expected, view_fragment('with_fragment', ['sample1', 'sample2'], $data));
77+
}
78+
79+
public function testViewFragmentFromLayout(): void
80+
{
81+
$data = [
82+
'testString1' => 'Hello World',
83+
'testString2' => 'Hello World',
84+
];
85+
86+
$expected = "Page bottom";
87+
88+
$this->assertSame($expected, view_fragment('with_fragment', 'sample0', $data));
89+
}
6690
}

0 commit comments

Comments
 (0)