Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit 0ffff1a

Browse files
committed
Merge pull request #62 from fduarte42/master
Fixed missing root using layout viewhelper
2 parents e92f8c3 + 5b628a0 commit 0ffff1a

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

src/ZendViewRenderer.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Zend\Expressive\Template\Exception;
1515
use Zend\Expressive\Template\TemplatePath;
1616
use Zend\Expressive\Template\TemplateRendererInterface;
17+
use Zend\View\Helper;
1718
use Zend\View\Model\ModelInterface;
1819
use Zend\View\Model\ViewModel;
1920
use Zend\View\Renderer\PhpRenderer;
@@ -193,8 +194,15 @@ private function createModel($name, $params)
193194
*
194195
* @throws Exception\RenderingException if it encounters a terminal child.
195196
*/
196-
private function renderModel(ModelInterface $model, RendererInterface $renderer) : string
197-
{
197+
private function renderModel(
198+
ModelInterface $model,
199+
RendererInterface $renderer,
200+
ModelInterface $root = null
201+
) : string {
202+
if (! $root) {
203+
$root = $model;
204+
}
205+
198206
foreach ($model as $child) {
199207
if ($child->terminate()) {
200208
throw new Exception\RenderingException('Cannot render; encountered a child marked terminal');
@@ -206,7 +214,13 @@ private function renderModel(ModelInterface $model, RendererInterface $renderer)
206214
}
207215

208216
$child = $this->mergeViewModel($child->getTemplate(), $child);
209-
$result = $this->renderModel($child, $renderer);
217+
218+
if ($child !== $root) {
219+
$viewModelHelper = $renderer->plugin(Helper\ViewModel::class);
220+
$viewModelHelper->setRoot($root);
221+
}
222+
223+
$result = $this->renderModel($child, $renderer, $root);
210224

211225
if ($child->isAppend()) {
212226
$oldResult = $model->{$capture};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php $this->layout('zendview-layout2'); ?>
2+
3+
<h1>This is a template file for ZendView</h1>

test/ZendViewRendererTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,20 @@ public function testRenderChildWithDefaultParameter()
606606

607607
static::assertEquals($content, $result);
608608
}
609+
610+
public function testChangeLayoutInTemplate()
611+
{
612+
$renderer = new ZendViewRenderer();
613+
$renderer->addPath(__DIR__ . '/TestAsset');
614+
615+
$result = $renderer->render('zendview-change-layout', ['layout' => 'zendview-layout']);
616+
617+
$contentChild = file_get_contents(__DIR__ . '/TestAsset/zendview-change-layout.phtml');
618+
$contentChild = str_replace("<?php \$this->layout('zendview-layout2'); ?>\n", '', $contentChild);
619+
620+
$content = file_get_contents(__DIR__ . '/TestAsset/zendview-layout2.phtml');
621+
$content = str_replace("<?= \$this->content ?>\n", $contentChild, $content);
622+
623+
static::assertEquals($content, $result);
624+
}
609625
}

0 commit comments

Comments
 (0)