views:

90

answers:

1

Hello all

I am developing a webapp using Cakephp. I have created a default layout for the website. I created sections like header, footer, content.

Now some of the pages in my content section have same structure. These are part of a controller with various views defined for each page. What view template should I be using here? Layouts or Elements?

In case I use layout, I will be using the controller to decide the layout I will use, but won't that change the layout for whole website?

Regards

+1  A: 

This is really a matter of preference. I try to think of layout as the "structure" of the page -- so a header, placeholder for left nav, placeholder for top nav, footer. That kind of thing. I define those sections in the layout.

I'd recommend the "pages in my content section [that] have the same structure" be your layout. As you mentioned, you can switch layouts in the controller, but any similarly structured pages (pages that will look the same as each other once you've removed the content) I would have share a layout.

Another example of layout switching could be for logged in versus anonymous users. For a logged in user, websites frequently put a "Welcome, $name" message at the top, along with links to My Account, sign out, etc. I often find myself using a different layout for logged in versus anon users. ALso, in that situation, I use the beforeFilter() method to set the layout for a given controller, since I know whether the user is logged in or not when that callback is executed.

Sorry for the rambling, but this is a bit of a gray area (when to use a layout versus different elements).

Travis Leleu
@Travis - Thanks for this rambling :). One thing about my problem is that the alleged pages (that have the same structure) have their content structured in the same way. The header, footer, menus and all are still the same as main layout. In this case, is there any way where I can use a layout within a layout?
ShiVik
I'd put those into elements, which you can include from the layout. Semantically it just makes a little more sense -- the layout is the skeleton, which can be made up of multiple components (elements). It mixes the metaphor a bit to include a layout from within another one.
Travis Leleu