different layouts on different views cakephp 2.0

I have this on my controller:

class EmployeeAccountsController extends AppController
{
var $layout = 'login';

public function login()
{

}
public function main_board()
{

}

}

what I want to do is that to assign different layouts when I call the view for login() and the view for the main_board.

login() must have layout:login.ctp main_board() must have layout:main_board()

is it possible for a single controller to have different layout?

Upvotes: 1

Views: 3640

Answers (1)

Aurimas Ličkus
Aurimas Ličkus

Reputation: 10074

Yes it is, and callback beforeFilter into your controller like:

class EmployeeAccountsController extends AppController {

  protected $layout = 'login';

  public function beforeFilter() {
      parent::beforeFilter(); //call parent before filter

      $this->layout = $this->layout;
  }
}

In this case EmployeeAccounts controller will have login layout for all views.

EDIT: just define layout especially for action

public function action() {   
 $this->layout = 'layout1';
}

public function action2() {   
 $this->layout = 'layout2';
}

Upvotes: 5

Related Questions