Renamed Controller::view to Controller::viewClass and Controller::view, View::view can now be used to change the view rendered by default. Parameters of Controller::render() and View::render() changed to eliminate redundancy. Closes #1520

This commit is contained in:
ADmad 2011-02-13 02:24:39 +05:30
parent e16d21eaa3
commit 681b9997b0
3 changed files with 54 additions and 41 deletions

View file

@ -134,6 +134,15 @@ class Controller extends Object {
*/
public $modelNames = array();
/**
* The name of the view file to render. The name specified
* is the filename in /app/views/<sub_folder> without the .ctp extension.
*
* @var string
* @link http://book.cakephp.org/view/962/Page-related-Attributes-layout-and-pageTitle
*/
public $view = null;
/**
* The name of the layout file to render the view inside of. The name specified
* is the filename of the layout in /app/views/layouts without the .ctp
@ -182,7 +191,7 @@ class Controller extends Object {
*
* @var string
*/
public $view = 'View';
public $viewClass = 'View';
/**
* Instance of the View created during rendering. Won't be set until after Controller::render() is called.
@ -815,21 +824,20 @@ class Controller extends Object {
/**
* Instantiates the correct view class, hands it its data, and uses it to render the view output.
*
* @param string $action Action name to render
* @param string $view View to use for rendering
* @param string $layout Layout to use
* @param string $file File to use for rendering
* @return string Full output string of view contents
* @link http://book.cakephp.org/view/980/render
*/
public function render($action = null, $layout = null, $file = null) {
public function render($view = null, $layout = null) {
$this->beforeRender();
$this->Components->trigger('beforeRender', array(&$this));
$viewClass = $this->view;
if ($this->view != 'View') {
$viewClass = $this->viewClass;
if ($this->viewClass != 'View') {
list($plugin, $viewClass) = pluginSplit($viewClass);
$viewClass = $viewClass . 'View';
App::import('View', $this->view);
App::import('View', $this->viewClass);
}
$this->request->params['models'] = $this->modelNames;
@ -865,7 +873,7 @@ class Controller extends Object {
$this->autoRender = false;
$this->View = $View;
return $this->response->body($View->render($action, $layout, $file));
return $this->response->body($View->render($view, $layout));
}
/**

View file

@ -87,6 +87,13 @@ class View extends Object {
*/
public $viewVars = array();
/**
* Name of view to use with this View.
*
* @var string
*/
public $view = null;
/**
* Name of layout to use with this View.
*
@ -234,7 +241,7 @@ class View extends Object {
* @var array
*/
private $__passedVars = array(
'viewVars', 'autoLayout', 'ext', 'helpers', 'layout', 'name',
'viewVars', 'autoLayout', 'ext', 'helpers', 'view', 'layout', 'name',
'layoutPath', 'viewPath', 'request', 'plugin', 'passedArgs', 'cacheAction'
);
@ -353,8 +360,7 @@ class View extends Object {
}
/**
* Renders view for given action and layout. If $file is given, that is used
* for a view filename (e.g. customFunkyView.ctp).
* Renders view for given view file and layout.
*
* Render triggers helper callbacks, which are fired before and after the view are rendered,
* as well as before and after the layout. The helper callbacks are called
@ -366,14 +372,12 @@ class View extends Object {
*
* If View::$autoRender is false and no `$layout` is provided, the view will be returned bare.
*
* @param string $action Name of action to render for, this will be used as the filename to render, unless
* $file is give as well.
* @param string $view Name of view file to use
* @param string $layout Layout to use.
* @param string $file Custom filename for view. Providing this will render a specific file for the given action.
* @return string Rendered Element
* @throws CakeException if there is an error in the view.
*/
public function render($action = null, $layout = null, $file = null) {
public function render($view = null, $layout = null) {
if ($this->hasRendered) {
return true;
}
@ -382,11 +386,7 @@ class View extends Object {
}
$this->output = null;
if ($file != null) {
$action = $file;
}
if ($action !== false && $viewFileName = $this->_getViewFileName($action)) {
if ($view !== false && $viewFileName = $this->_getViewFileName($view)) {
$this->Helpers->trigger('beforeRender', array($viewFileName));
$this->output = $this->_render($viewFileName);
$this->Helpers->trigger('afterRender', array($viewFileName));

View file

@ -343,7 +343,7 @@ class TestComponent extends Object {
*/
function beforeRender($controller) {
if ($this->viewclass) {
$controller->view = $this->viewclass;
$controller->viewClass = $this->viewclass;
}
}
}
@ -641,7 +641,7 @@ class ControllerTest extends CakeTestCase {
'views' => array(TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'test_app' . DS . 'views'. DS)
), true);
$request = new CakeRequest('controller_posts/index');
$request->params['action'] = 'index';
$Controller = new Controller($request, $this->getMock('CakeResponse'));
$Controller->viewPath = 'posts';
@ -649,8 +649,13 @@ class ControllerTest extends CakeTestCase {
$result = $Controller->render('index');
$this->assertPattern('/posts index/', $result);
$Controller->view = 'index';
$result = $Controller->render();
$this->assertPattern('/posts index/', $result);
$result = $Controller->render('/elements/test_element');
$this->assertPattern('/this is the test element/', $result);
$Controller->view = null;
$Controller = new TestController($request);
$Controller->helpers = array('Html');