Skip to content

Commit

Permalink
Added accessor method getView() to ViewVarsTrait.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 12, 2014
1 parent 92e79f2 commit 68b6628
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 8 deletions.
8 changes: 3 additions & 5 deletions src/Controller/Controller.php
Expand Up @@ -179,7 +179,7 @@ class Controller implements EventListener {
*
* @var string
*/
public $viewClass = 'Cake\View\View';
public $viewClass = null;

/**
* The path to this controllers view templates.
Expand Down Expand Up @@ -388,7 +388,7 @@ public function setRequest(Request $request) {
$this->autoRender = false;
}
if (!empty($request->params['bare'])) {
$this->autoLayout = false;
$this->getView()->autoLayout = false;
}
}

Expand Down Expand Up @@ -587,10 +587,8 @@ public function render($view = null, $layout = null) {
return $this->response;
}

$this->View = $this->createView();

$this->autoRender = false;
$this->response->body($this->View->render($view, $layout));
$this->response->body($this->getView()->render($view, $layout));
return $this->response;
}

Expand Down
5 changes: 3 additions & 2 deletions src/View/Cell.php
Expand Up @@ -78,7 +78,7 @@ abstract class Cell {
*
* @var string
*/
public $viewClass = 'Cake\View\View';
public $viewClass = null;

/**
* The theme name that will be used to render.
Expand Down Expand Up @@ -153,7 +153,8 @@ public function render($template = null) {
$template = $this->template;
}

$this->View = $this->createView();
$this->View = null;
$this->getView();

$this->View->layout = false;
$className = explode('\\', get_class($this));
Expand Down
29 changes: 28 additions & 1 deletion src/View/ViewVarsTrait.php
Expand Up @@ -31,11 +31,38 @@ trait ViewVarsTrait {
*/
public $viewVars = [];

/**
* Get view instance
*
* @param string $viewClass View class name or null to use $viewClass
* @return \Cake\View\View
*/
public function getView($viewClass = null) {
if ($viewClass === null && $this->View) {
return $this->View;
}

if ($viewClass !== null && $viewClass !== $this->viewClass) {
$this->viewClass = $viewClass;
unset($this->View);
}
if ($this->viewClass === null) {
$this->viewClass = App::className('App', 'View', 'View');
if ($this->viewClass === false) {
$this->viewClass = 'Cake\View\View';
}
}
if (empty($this->View)) {
$this->View = $this->createView();
}
return $this->View;
}

/**
* Constructs the view class instance based on object properties.
*
* @param string $viewClass Optional namespaced class name of the View class to instantiate.
* @return View
* @return Cake\View\View
*/
public function createView($viewClass = null) {
if ($viewClass === null) {
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase/Controller/ControllerTest.php
Expand Up @@ -362,9 +362,11 @@ public function testRender() {
$this->assertRegExp('/posts index/', (string)$result);

$Controller->view = 'index';
$Controller->getView()->hasRendered = false;
$result = $Controller->render();
$this->assertRegExp('/posts index/', (string)$result);

$Controller->getView()->hasRendered = false;
$result = $Controller->render('/Element/test_element');
$this->assertRegExp('/this is the test element/', (string)$result);
$Controller->view = null;
Expand Down
23 changes: 23 additions & 0 deletions tests/test_app/TestApp/View/AppView.php
@@ -0,0 +1,23 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace TestApp\View;

use Cake\View\View;

/**
* App View class
*
*/
class AppView extends View {
}

0 comments on commit 68b6628

Please sign in to comment.