diff --git a/src/View/View.php b/src/View/View.php index 27293d4b655..2552626a912 100644 --- a/src/View/View.php +++ b/src/View/View.php @@ -554,6 +554,11 @@ public function render($view = null, $layout = null) return; } + if ($layout !== null) { + $defaultLayout = $this->layout; + $this->layout = $layout; + } + if ($view !== false && $viewFileName = $this->_getViewFileName($view)) { $this->_currentType = static::TYPE_VIEW; $this->dispatchEvent('View.beforeRender', [$viewFileName]); @@ -561,12 +566,13 @@ public function render($view = null, $layout = null) $this->dispatchEvent('View.afterRender', [$viewFileName]); } - if ($layout === null) { - $layout = $this->layout; + if ($this->layout && $this->autoLayout) { + $this->Blocks->set('content', $this->renderLayout('', $this->layout)); } - if ($layout && $this->autoLayout) { - $this->Blocks->set('content', $this->renderLayout('', $layout)); + if ($layout !== null) { + $this->layout = $defaultLayout; } + $this->hasRendered = true; return $this->Blocks->get('content'); } diff --git a/tests/TestCase/View/ViewTest.php b/tests/TestCase/View/ViewTest.php index 8b58d511b34..764b32348f7 100644 --- a/tests/TestCase/View/ViewTest.php +++ b/tests/TestCase/View/ViewTest.php @@ -1290,6 +1290,27 @@ public function testRenderUsingViewProperty() $this->assertRegExp('/Add User/', $result); } + /** + * Test that layout set from view file takes precedence over layout set + * as argument to render(). + * + * @return void + */ + public function testRenderUsingLayoutArgument() + { + $error = new \PDOException(); + $error->queryString = 'this is sql string'; + $message = 'it works'; + + $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView'); + $View->set(compact('error', 'message')); + $View->viewPath = 'Error'; + + $result = $View->render('pdo_error', 'error'); + $this->assertRegExp('/this is sql string/', $result); + $this->assertRegExp('/it works/', $result); + } + /** * Test render()ing a file in a subdir from a custom viewPath * in a plugin.