Tamas
Tamas

Reputation: 11214

CakePHP - display view/element in different ways

I'm fairly new to CakePHP so please do excuse me if it's a somewhat ridiculous question. I've already figured out that if I want to use a controller on multiple pages I need to use Elements. So I've created an element that prints out various pieces of information. The question that I have is -- what and, how to modify the Element so that on one page I can print out the values from my controller and on another page I can put the values into a dropdown for example.

thanks!

Upvotes: 0

Views: 630

Answers (1)

swiecki
swiecki

Reputation: 3483

The conventions are as follows: A controller contains all the actions that interact with a data model. For example, the Users controller will interact with the User model, by providing say Login, Logout, and Edit Profile actions. Each action is simply a public method in the controller. These actions set pieces of data that are then used when the html is rendered.

Views are the corresponding pieces of template language and html that render the data passed from the controller for each action.

Layouts are a way of having views that don't repeat themselves too much. If your pages have similar structure, as most websites do, then using a layout, in which the view is rendered, helps cut down on code.

An element is simply one part of a view that gets re-used. For example, if all of your views have a header on top, for your convenience you can put that header code into an element and simply call the element at the top of your layout, and it will be in each view. This also eliminates repetition in code because if you need to change that header, you can just modify header.ctp in the elements folder, and not have to modify each view or each layout.

Upvotes: 1

Related Questions