3.5.2.3 Page-related Attributes: $layout and $pageTitle
A few attributes exist in CakePHP controllers that give you control over how your view is set inside of a layout.
The $layout attribute can be set to the name of a layout saved in /app/views/layouts. You specify a layout by setting $layout equal to the name of the layout file minus the .ctp extension. If this attribute has not been defined, CakePHP renders the default layout, default.ctp. If you haven’t defined one at /app/views/layouts/default.ctp, CakePHP’s core default layout will be rendered.
<?php
// Using $layout to define an alternate layout
class RecipesController extends AppController {
function quickSave() {
$this->layout = 'ajax';
}
}
?>
<?php// Using $layout to define an alternate layoutclass RecipesController extends AppController {function quickSave() {$this->layout = 'ajax';}}?>
You can also change the title of the page (that is located in the bar at the top of your browser) using $pageTitle. In order for this to work properly, your layout needs to include the $title_for_layout variable, at least between the <title> and </title> tags in the head of the HTML document.
<?php
// Using $pageTitle to define the page title
class RecipesController extends AppController {
function quickSave() {
$this->pageTitle = 'My search engine optimized title';
}
}
?>
<?php// Using $pageTitle to define the page titleclass RecipesController extends AppController {function quickSave() {$this->pageTitle = 'My search engine optimized title';}}?>
You can also set the page title from the view using $this->pageTitle (You must include the $this-> part.) This is recommended, as it better separates the logic from the layout and content. For a static page you must use $this->pageTitle in the view if you want a custom title.
If $this->pageTitle is not set, a title will be automatically generated based on the controller name, or the view file name in the case of a static page.
