If I have a few views, add.ctp, edit.ctp all using the same code for the form.
I have created another view file called _form.ctp
For displaing same part we call $this->renderPartial(‘_form’);

How I implement it?

All we need is overload View class with renderPartial function implementation. The goal of function is call renderElement with correct path to view.

<?php
class ExtView extends 
View 
{    

    function renderPartial($name$params = array(), $loadHelpers false) {
        if ((
strpos($name‘\\’)===false) && (strpos($name‘/’)===false
)) {
            
$name ‘..’ DS Inflector::underscore($this->name) . DS $name
;            
        }
        return 
$this->renderElement($name$params$loadHelpers
);
    }

    
}
?>

Now in app_controller all I need is set default view class in beforeRender callback:

<?php
class AppController extends Controller 
{
   
    function 
beforeRender
() {
        
$this->view=‘Ext’
;
        return 
true
;
    }    
    
}
?>

Advertisements