CakePHP Layouts & Elements

Facebooktwitterredditpinterestlinkedinmailby feather

A layout includes a presentation code that can be wrapped out in your view. It means that what you want to see in your view should be well-positioned in layout. When you create a layout file, then you have to tell CakePHP where you can place the data of your view code, which can be rendered by the controller, and CakePHP’s default layout is located at src/Template/Layout/default.ctp. While creating a layout for your application, you have to make sure that it includes this syntax “$this->fetch(‘content’);” for fetching the content.


You can also define php syntax like this:

If you have used this syntax “<?= ?>” for printing any text, then you don’t need to write php as well echo; it considered by default in php.

The default file loaded all the content of the template’s action file that can be rendered on the browser. The dafault.ctp file contains the default page layout for the CakePHP application, and if you want to modify the page, then you can change by going to Layout/default.ctp

The above function exists in default.ctp file for fetching the content in controller.php file with the use of fetch(‘content’) function.

Now we are going to create a program using the layout in the controller file. For this, we have to require three files.

  1. SecondController.php file
  2. fun3.ctp file
  3. And the last fun3layout.ctp file

This is the fun3layout.ctp (default file) which loads all the content of controller.php file.

For loading the layout function in a controller, we have to create a constructor and pass the fun3layout.ctp file in the layout function of the Controller, or you can modify the default.ctp file according to your need, which is defined in layout/default.ctp and then calls it at the controller.

Here, we have created the SecondController.php file in the Controller folder.

Now we have to create fun3.ctp file in Second folder of Template folder and write the given below program in your fun3.ctp file.


CakePHP Layouts & Elements

View Elements

Elements are the small block of layout code, that can be re-used in any page by calling in default.ctp  or layout.ctp file to avoid re-writing code.

  • It is like ad boxes, sliders, navigation, etc.
  • It can be considered as mini-view.

Here, we have created the SecondController.php file in the Controller folder.

This is the file of fun3layout.ctp at src\Template\Layout\fun3layout.ctp file.

Now we have to create headerElement.ctp file for setting the header in web application.

Similary, we have created “footerElement.ctp” file for set the footer in web page.


CakePHP Layouts

View Events

Here we have described several callbacks/events that can be used in a view. In CakePHP, event controllers are helpful in performing multiple tasks before or after an action. CakePHP contains a list of viewing events as follows.

Events Function and Description

  • Helper::beforeRender(Events $event, $viewFile) 

The beforeRender function will be called after the Controller’index, but before the controller renders view and layout. This function will render the file as an argument.

  • Helper::beforeRenderFile(Event $event, $viewFile)

The function will call before each view file is rendered. This function will include elements, view, parent view, and layouts.

  • Helper::afterRenderFile(Event $event, $viewFile, $content)

The method will call after each View file is rendered. This function includes elements, views, parent views, and layouts. A callback/events can be modified and return the $content for representing the content in the browser.

  • Helper::afterRender(Event $event, $viewFile) 

This method will be called after the view file is rendered, but before the layout rendering method start.

  • Helper::beforeLayout(Event $event, $layoutFile)

This method will be provided before the layout starts and get the filename as an argument.

  • Helper::afterLayout(Event $event, $layoutFile)

The view helper function will be called after the layout render is complete and get the filename as an argument.

Facebooktwitterredditpinterestlinkedinmailby feather