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

Commit 26962d3

Browse files
committed
Merge branch 'hotfix/62'
Close #62 Fixes zendframework/zend-expressive-skeleton#68
2 parents e92f8c3 + c3eb981 commit 26962d3

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.0.2 - TBD
5+
## 2.0.2 - 2019-01-14
66

77
### Added
88

@@ -22,7 +22,8 @@ All notable changes to this project will be documented in this file, in reverse
2222

2323
### Fixed
2424

25-
- Nothing.
25+
- [#62](https://github.com/zendframework/zend-expressive-zendviewrenderer/pull/62) provides a fix to allow setting the layout via the `layout()` view helper
26+
within a view script.
2627

2728
## 2.0.1 - 2018-08-13
2829

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)