Skip to content

Commit

Permalink
Fix: Nested sections rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
iRedds committed Apr 29, 2021
1 parent 4c37534 commit 131daf4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
30 changes: 18 additions & 12 deletions system/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class View implements RendererInterface
* The name of the current section being rendered,
* if any.
*
* @var string|null
* @var array<string>
*/
protected $currentSection;
protected $sectionStack = [];

/**
* Constructor
Expand Down Expand Up @@ -227,7 +227,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
// When using layouts, the data has already been stored
// in $this->sections, and no other valid output
// is allowed in $output so we'll overwrite it.
if (! is_null($this->layout) && empty($this->currentSection))
if (! is_null($this->layout) && $this->sectionStack === [])
{
$layoutView = $this->layout;
$this->layout = null;
Expand Down Expand Up @@ -402,35 +402,41 @@ public function extend(string $layout)
/**
* Starts holds content for a section within the layout.
*
* @param string $name
* @param string $name Section name
*
* @return void
*/
public function section(string $name)
public function section(string $name): void
{
$this->currentSection = $name;
$this->sectionStack[] = $name;

ob_start();
}

/**
* Captures the last section
*
* @return void
* @throws RuntimeException
*/
public function endSection()
public function endSection(): void
{
$contents = ob_get_clean();

if (empty($this->currentSection))
if ($this->sectionStack === [])
{
throw new RuntimeException('View themes, no current section.');
}

$section = array_pop($this->sectionStack);

// Ensure an array exists so we can store multiple entries for this.
if (! array_key_exists($this->currentSection, $this->sections))
if (! array_key_exists($section, $this->sections))
{
$this->sections[$this->currentSection] = [];
$this->sections[$section] = [];
}
$this->sections[$this->currentSection][] = $contents;

$this->currentSection = null;
$this->sections[$section][] = $contents;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion tests/system/View/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function testRenderSaveDataCover()
$this->assertEquals(true, $this->getPrivateProperty($view, 'saveData'));
}

public function testRenderSaveDataUseAflterSaveDataFalse()
public function testRenderSaveDataUseAfterSaveDataFalse()
{
$view = new View($this->config, $this->viewsDir, $this->loader);
$view->setVar('testString', 'test');
Expand All @@ -387,4 +387,17 @@ public function testCachedAutoDiscoverAndRender()
// this second renderings should go thru the cache
$this->assertStringContainsString($expected, $view->render('Nested/simple', ['cache' => 10]));
}

public function testRenderNestedSections()
{
$view = new View($this->config, $this->viewsDir, $this->loader);

$view->setVar('testString', 'Hello World');

$content = $view->render('nested_section');

$this->assertStringContainsString('<p>First</p>', $content);
$this->assertStringContainsString('<p>Second</p>', $content);
$this->assertStringContainsString('<p>Third</p>', $content);
}
}
14 changes: 14 additions & 0 deletions tests/system/View/Views/nested_section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php $this->extend('layout'); ?>

<?php $this->section('content'); ?>
<p>Second</p>

<?php $this->section('content'); ?>
<p>First</p>
<?php $this->endSection(); ?>

<?php $this->endSection(); ?>

<?php $this->section('content'); ?>
<p>Third</p>
<?php $this->endSection(); ?>

0 comments on commit 131daf4

Please sign in to comment.