Setting Your Page Layout in CakePHP

I am not a pro in CakePHP, and in fact, i am just a beginner who wanted to explore it. When I was on my way to creating my goal, I stumbled upon a problem on how to change and then set the new page layout to be used. Honestly, the problem didn’t took me long to solve. I was lucky to have read the manual on the official site. So to cut the long story short, here is the list of steps which I did to come up with my new and first page layout on my CakePHP inspired website and you might consider following this little manual, too.

1. First, design the layout that you wanted to implement and use. Assuming we have the simple code below as your desired page layout.
<html>
<head>
<title></title>
</head>
<body>

</body>
</html>

2. After having established the base, let us now put the code that will show the title of your page. We will use $title_for_layout to do the job. So your head section will now look like this:
<head>
<title><?php echo $title_for_layout?></title>
</head>

3. Next inline would be displaying the contents. We will use $content_for_layout. It will tell CakePHP where to place the code for your views. Our code now will look like this one.
<html>
<head>
<title><?php echo $title_for_layout?></title>
</head>
<body>
<?php echo $content_for_layout ?>
</body>
</html>

4. Then save it as app/views/layouts/default.ctp.

Next topic would be creating and using multitle layouts for single website. Please feel free to share your views which you think can help us beginners make a step higher on CakePHP.

7 Responses to “Setting Your Page Layout in CakePHP”

  1. ajk Says:
    where do you save the definitions variables ($title_for_layout)?
  2. cyrose Says:
    Hi ajk,

    About your question, $title_for_layout will get the filename of your views files minus the “.ctp” and will use it as your title. This is true if your views files are located inside app/views/pages folder. But if you have created your own folder inside views, say app/views/registers, the name of the folder will now be the page title for all views files inside app/views/registers.

    However, if you wish to customize your title, you can do that using $pageTitle controller variable. You will put it on your controller file. To show what I mean, here’s a sample code:

    class RegistersController extends AppController {
    var $name = ‘Registers’;

    function index() {
    $this->pageTitle = ‘Manage Admin Panel’;
    $this->checkSession();
    }

    So now, if you will open the page say url/registers/index on your browser, the tilte bar will show ‘Manage Admin Panel’.

    Thanks,
    Cyrose
  3. Smart Pad Says:
    To ajk, I think the title can be set in your controller by the following;

    $this->set(‘title’, ‘your page title’);
  4. kiran Says:
    hi i have to send an array containing category and brand names to my layout file – default.ctp. My html – section has some javasript code. I want to place content from above array into that js. like – mm_menu = “location:<?=cats['cats']['name']“. is it possible? how do i do it?
  5. rachel Says:
    @Kiran, you set variables in layout just like how you do it in any views file. So for this instance, you can use the

    $cats['cats']['name'] = ‘whatever here’;
    $this->set(‘cats’, $cats)

    to send any info in your default.ctp and display it in the layout like

    <?=$cats['cats']['name'] ?>
  6. mzee.richo Says:
    Nice stuff . You kids or guys are very bright . i was having issues with stuff . nice stuff
  7. How to create custom layout for your website in CakePHP « Myles Kadusale’s Blog Says:
    [...] Other links related Reformatting the default layout Setting up the page layout [...]

Add Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>