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.
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.


July 21st, 2008 at 7:24 am where do you save the definitions variables ($title_for_layout)?
July 21st, 2008 at 8:07 pm 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
August 4th, 2008 at 2:50 am To ajk, I think the title can be set in your controller by the following;
$this->set(‘title’, ‘your page title’);
October 11th, 2008 at 7:36 am 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?
October 11th, 2008 at 9:55 am @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'] ?>
October 24th, 2008 at 6:36 am Nice stuff . You kids or guys are very bright . i was having issues with stuff . nice stuff
May 14th, 2009 at 5:00 am [...] Other links related Reformatting the default layout Setting up the page layout [...]