views:

633

answers:

1

If my whole site is using the default.ctp layout specified in apps/view/layouts/default.ctp, how do I change only the home page layout to use homepage.ctp and leave the rest of the site using default.ctp?

+3  A: 

Copy the /cake/libs/controller/pages_controller.php into your /app/controller/ dir and do either of the following:

  • Add a line towards the end of display() to switch the layout if 'home' is requested:
    if ($page == 'home') $this->layout = 'homepage';
  • Create a home() method (or named however you like) in which you set $this->layout and re-route the / route in /app/config/routes.php to use this new method.

Edit:
In summary, you need some custom method in which you'll set $this->layout = 'homepage', that's all. You can do this in any of your controllers at any point, reusing the PagesController is just the most convenient and conventional way to do it in Cake.

deceze
Isn't overwriting the class a little smarter. I also guess that `$this->layout` can be called from some other function, something `init`-like.
Franz
Actually, you don't even need the original `PagesController` - you can use routes to point to a custom controller and set `$this->layout` from there
K Prime
@Franz What do you mean by "overwriting"?
deceze
@K Sure you can, but its a good idea to keep only the PagesController for static pages as per convention.
deceze
Cool, i just got this to work using the first point. Although I also noticed that the custom cms that another guy here at work wrote overrides this setting. So basically I just had to specify a template for the homepage through the cms. When I'm not using the cms though, I'll be sure to use this. Thanks for the help guys.
Bradley
For those new to cakephp please note that if using deceze's first solution the $this->layout = 'homepage'; line should go before the $this->render(join('/', $path)); line (the last line in the display function) as otherwise the layout won't be updated from the default one.
Loftx