Skip to content

Commit 7ba9bab

Browse files
committed
Fix: Nested sections rendering
1 parent 4c37534 commit 7ba9bab

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

system/View/View.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ class View implements RendererInterface
120120
* The name of the current section being rendered,
121121
* if any.
122122
*
123-
* @var string|null
123+
* @var array<string>
124124
*/
125-
protected $currentSection;
125+
protected $sectionStack = [];
126126

127127
/**
128128
* Constructor
@@ -227,7 +227,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
227227
// When using layouts, the data has already been stored
228228
// in $this->sections, and no other valid output
229229
// is allowed in $output so we'll overwrite it.
230-
if (! is_null($this->layout) && empty($this->currentSection))
230+
if (! is_null($this->layout) && $this->sectionStack === [])
231231
{
232232
$layoutView = $this->layout;
233233
$this->layout = null;
@@ -402,35 +402,41 @@ public function extend(string $layout)
402402
/**
403403
* Starts holds content for a section within the layout.
404404
*
405-
* @param string $name
405+
* @param string $name Section name
406+
*
407+
* @return void
406408
*/
407-
public function section(string $name)
409+
public function section(string $name): void
408410
{
409-
$this->currentSection = $name;
411+
$this->sectionStack[] = $name;
410412

411413
ob_start();
412414
}
413415

414416
/**
417+
* Captures the last section
418+
*
419+
* @return void
415420
* @throws RuntimeException
416421
*/
417-
public function endSection()
422+
public function endSection(): void
418423
{
419424
$contents = ob_get_clean();
420425

421-
if (empty($this->currentSection))
426+
if ($this->sectionStack === [])
422427
{
423428
throw new RuntimeException('View themes, no current section.');
424429
}
425430

431+
$section = array_pop($this->sectionStack);
432+
426433
// Ensure an array exists so we can store multiple entries for this.
427-
if (! array_key_exists($this->currentSection, $this->sections))
434+
if (! array_key_exists($section, $this->sections))
428435
{
429-
$this->sections[$this->currentSection] = [];
436+
$this->sections[$section] = [];
430437
}
431-
$this->sections[$this->currentSection][] = $contents;
432438

433-
$this->currentSection = null;
439+
$this->sections[$section][] = $contents;
434440
}
435441

436442
/**

tests/system/View/ViewTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public function testRenderSaveDataCover()
367367
$this->assertEquals(true, $this->getPrivateProperty($view, 'saveData'));
368368
}
369369

370-
public function testRenderSaveDataUseAflterSaveDataFalse()
370+
public function testRenderSaveDataUseAfterSaveDataFalse()
371371
{
372372
$view = new View($this->config, $this->viewsDir, $this->loader);
373373
$view->setVar('testString', 'test');
@@ -387,4 +387,23 @@ public function testCachedAutoDiscoverAndRender()
387387
// this second renderings should go thru the cache
388388
$this->assertStringContainsString($expected, $view->render('Nested/simple', ['cache' => 10]));
389389
}
390+
391+
public function testRenderNestedSections()
392+
{
393+
$view = new View($this->config, $this->viewsDir, $this->loader);
394+
395+
$view->setVar('testString', 'Hello World');
396+
397+
$content = $view->render('nested_section');
398+
399+
$this->assertStringContainsString('<p>First</p>', $content);
400+
$this->assertStringContainsString('<p>Second</p>', $content);
401+
$this->assertStringContainsString('<p>Third</p>', $content);
402+
403+
$secondPosition = strpos($content, '<p>Second</p>');
404+
405+
$this->assertLessThan(strpos($content, '<p>First</p>'), $secondPosition);
406+
$this->assertLessThan($secondPosition, strpos($content, '<p>Third</p>'));
407+
408+
}
390409
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php $this->extend('layout'); ?>
2+
3+
<?php $this->section('content'); ?>
4+
<p>Second</p>
5+
6+
<?php $this->section('content'); ?>
7+
<p>First</p>
8+
<?php $this->endSection(); ?>
9+
10+
<?php $this->endSection(); ?>
11+
12+
<?php $this->section('content'); ?>
13+
<p>Third</p>
14+
<?php $this->endSection(); ?>

0 commit comments

Comments
 (0)