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

Commit 043d88d

Browse files
committed
Merge branch 'hotfix/43' into develop
Forward port #43
2 parents 3718343 + 73c4f97 commit 043d88d

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ All notable changes to this project will be documented in this file, in reverse
4545

4646
### Fixed
4747

48-
- Nothing.
48+
- [#43](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/43)
49+
ensures that if a view model provided to the renderer contains child view
50+
models, then it will properly merge variables pulled from the child model.
51+
Previously, an error would occur due to an attempt to merge either a null or
52+
an object where it expected an array.
4953

5054
## 1.4.0 - 2017-03-14
5155

src/ZendViewRenderer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,10 @@ private function getNamespacedResolver(AggregateResolver $aggregate)
295295
*/
296296
private function mergeViewModel($name, ModelInterface $model)
297297
{
298-
$params = $this->mergeParams($name, $model->getVariables());
298+
$params = $this->mergeParams(
299+
$name,
300+
$this->normalizeParams($model->getVariables())
301+
);
299302
$model->setVariables($params);
300303
$model->setTemplate($name);
301304
return $model;

test/ZendViewRendererTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,32 @@ public function testWillRenderAViewModel()
468468
$content = str_replace('<?php echo $name ?>', 'Zend', $content);
469469
$this->assertEquals($content, $result);
470470
}
471+
472+
public function testCanRenderWithChildViewModel()
473+
{
474+
$path = __DIR__ . '/TestAsset';
475+
$renderer = new ZendViewRenderer();
476+
$renderer->addPath($path);
477+
478+
$viewModelChild = new ViewModel();
479+
$viewModelChild->setTemplate('zendview-null');
480+
481+
$viewModelParent = new ViewModel();
482+
$viewModelParent->setVariables([
483+
'layout' => 'zendview-layout',
484+
]);
485+
$viewModelParent->addChild($viewModelChild, 'name');
486+
487+
$result = $renderer->render('zendview', $viewModelParent);
488+
489+
$content = file_get_contents(sprintf('%s/zendview-null.phtml', $path));
490+
$contentParent = file_get_contents(sprintf('%s/zendview.phtml', $path));
491+
$contentParentLayout = file_get_contents(sprintf('%s/zendview-layout.phtml', $path));
492+
493+
// trim is used here, because rendering engine is trimming content too
494+
$content = trim(str_replace('<?php echo $name ?>', $content, $contentParent));
495+
$content = str_replace('<?= $this->content ?>', $content, $contentParentLayout);
496+
497+
$this->assertEquals($content, $result);
498+
}
471499
}

0 commit comments

Comments
 (0)