[CakePHP]How to get View file name in element or layout


[cakephp 1.3.3]
I introduce how to get to know view name in element or layout that is specified by $this->render in controller.

For example, with the contact form, as seen below, many times with one action the view is specifed.

function contact() {
        //first view or return selected
        if (!empty($this->params['form']['return']) || empty($this->data)) {
            /* getting data required by form process */
        }
        //confirm view or error view
        elseif (!empty($this->params['form']['confirm'])) {
            /* validating process */
            $this->render('contact_confirm');
        }
        //finish
        else {
            /* saving data process */
            $this->render('contact_done');
        }       
}


 


But depending on the value of the view, there are times when I want to change the operation of element or layout.

At that time, if you render method override the app_controller, from the element or layout in $this->params[‘view’] you can subtract the value and it’s convenient.

function render($action = null, $layout = null, $file = null) {
     if ($action !== null) {
         $view = Inflector::underscore($action);
     }
    else {
         $view = Inflector::underscore($this->action);
    }
    $this->params['view'] = $view;
    
    if (!$controller = $this->params['controller']) {
         return false;
    }
    return parent::render($action, $layout, $file);
}


 

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
This entry was posted in CakePHP. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>