How To Define Layout In Cake PHP

Layout refers the hows your website looks, the Layout define your header and footer with content area.Anything you want to see in all of your views should be placed in a layout.Layout files should be placed in /app/views/layouts.
You can define you custom layout as well and placed into '/app/views/layouts' folder.Cake PHP also provide functionality to override default layout,to override layout you need to create a default.ctp file and placed into layout folder.

now whenever controller rendered view you content will placed inside of the default layout.

The cake PHP have inbuilt following Layouts for html content:

1- default layout
2- ajax
3- flash

As per layout name, you know the default layout responsible for general html render, whereas ajax is responsible for handle ajax request and send response simple content without header and footer.The flash layout is used for messages shown by the controllers flash() method.

How to define custom layout in cake PHP:
create custom layout we make sure the layout folder should be includes a place for $content_for_layout (and optionally, $title_for_layout).
Simple example of custom layout is below:
Step 1: create custom.ctp fila nd placed into layout folder.

$scripts_for_layout : This variable contains any external files and scripts included with the built-in HTML helper.
$content_for_layout: This variable contains the view.
$title_for_layout: This variable contains the page title.

Step 2:Call this layout in controller action.

title_for_layout : This variable use to set the title for view.
$this->layout : This variable used for set the layout file.

Leave a Reply

Your email address will not be published. Required fields are marked *