Denis Rendler
Denis Rendler

Reputation: 182

Load layout element based on view in CakePHP

I have a sidebar defined in my main layout which most of the time will display the login form. After the user is logged in I need to remove that form and replace it with user data. I also need to change that sidebar when viewing the support section to show the sub-sections.

Do I need to move the element loading to each view or is there another way?

Thanks in advance, Denis

Upvotes: 0

Views: 932

Answers (1)

ori
ori

Reputation: 7847

Bottom line is you're gonna need an if($supportpage){} elseif($loggedin){} else{} block. If you don't want to put it in your layout file you could create an element for each option and then set() the correct one from the app_controller:

if ($supportpage) $sidebar = 'support';
elseif ($loggedin) $sidebar = 'loggedin';
else $sidebar = 'notloggedin';

$this->set(compact($sidebar));

And then put $this->element($sidebar) in your layout.

Upvotes: 1

Related Questions