Navigation Menu

Skip to content

Commit

Permalink
Allow overriding layout passed as argument by setting View::$layout i…
Browse files Browse the repository at this point in the history
…n template
  • Loading branch information
ADmad committed Aug 12, 2015
1 parent c299d77 commit ced5437
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/View/View.php
Expand Up @@ -554,19 +554,25 @@ 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]);
$this->Blocks->set('content', $this->_render($viewFileName));
$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');
}
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/View/ViewTest.php
Expand Up @@ -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.
Expand Down

0 comments on commit ced5437

Please sign in to comment.