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

Commit b375503

Browse files
committed
Fixed missing root using layout viewhelper
1 parent 33e8c7d commit b375503

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/ZendViewRenderer.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public function __construct(RendererInterface $renderer = null, $layout = null)
125125
* the constructor.
126126
*
127127
* @param array|ModelInterface|object $params
128+
*
129+
* @return string
128130
*/
129131
public function render(string $name, $params = []) : string
130132
{
@@ -191,10 +193,21 @@ private function createModel($name, $params)
191193
/**
192194
* Do a recursive, depth-first rendering of a view model.
193195
*
194-
* @throws Exception\RenderingException if it encounters a terminal child.
196+
* @param ModelInterface $model
197+
* @param RendererInterface $renderer
198+
* @param ModelInterface|null $root
199+
*
200+
* @return string
195201
*/
196-
private function renderModel(ModelInterface $model, RendererInterface $renderer) : string
197-
{
202+
private function renderModel(
203+
ModelInterface $model,
204+
RendererInterface $renderer,
205+
ModelInterface $root = null
206+
) : string {
207+
if (! $root) {
208+
$root = $model;
209+
}
210+
198211
foreach ($model as $child) {
199212
if ($child->terminate()) {
200213
throw new Exception\RenderingException('Cannot render; encountered a child marked terminal');
@@ -206,6 +219,12 @@ private function renderModel(ModelInterface $model, RendererInterface $renderer)
206219
}
207220

208221
$child = $this->mergeViewModel($child->getTemplate(), $child);
222+
223+
if ($child !== $root) {
224+
$viewModelHelper = $renderer->plugin('view_model');
225+
$viewModelHelper->setRoot($root);
226+
}
227+
209228
$result = $this->renderModel($child, $renderer);
210229

211230
if ($child->isAppend()) {
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,23 @@ 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+
$viewModelChild = new ViewModel();
616+
$viewModelChild->setTemplate('zendview-change-layout');
617+
618+
$result = $renderer->render('zendview-change-layout', ['layout' => 'zendview-layout']);
619+
620+
$contentChild = file_get_contents(__DIR__ . '/TestAsset/zendview-change-layout.phtml');
621+
$contentChild = str_replace("<?php \$this->layout('zendview-layout2'); ?>\n", '', $contentChild);
622+
623+
$content = file_get_contents(__DIR__ . '/TestAsset/zendview-layout2.phtml');
624+
$content = str_replace("<?= \$this->content ?>\n", $contentChild, $content);
625+
626+
static::assertEquals($content, $result);
627+
}
609628
}

0 commit comments

Comments
 (0)