{1660} - 3.5.2.3 Page-related Attributes: $layout and $pageTitle
A few attributes exist in CakePHP controllers that give you control over how your views 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. 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';}}?>
The $pageTitle controller attribute allows you to set the title of the rendered page. In order for this to work properly, your layout needs to include the $title_for_layout variable, at least between <title> tags in the head of the HTML document.
Just set $pageTitle to a string you’d like to see in the <title> of your document.
{5218} - 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.
Differences
| Lines: 1-26 | Lines: 1-41 | ||
| <title>Page-related Attributes: $layout and $pageTitle</title> | <title>Page-related Attributes: $layout and $pageTitle</title> | ||
| <p> | <p> | ||
| - | A few attributes exist in CakePHP controllers that give you control over how your views set inside of a layout. | + | A few attributes exist in CakePHP controllers that give you control over how your view is set inside of a layout. |
| </p> | </p> | ||
| <p> | <p> | ||
| - | 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. If you haven’t defined one at /app/views/layouts/default.ctp, CakePHP’s core default layout will be rendered. | + | The <code>$layout</code> attribute can be set to the name of a layout saved in <kbd>/app/views/layouts</kbd>. You specify a layout by setting <code>$layout</code> equal to the name of the layout file minus the <kbd>.ctp</kbd> extension. If this attribute has not been defined, CakePHP renders the default layout, <kbd>default.ctp</kbd>. If you haven’t defined one at <kbd>/app/views/layouts/default.ctp</kbd>, CakePHP’s core default layout will be rendered. |
| </p> | </p> | ||
| <pre> | <pre> | ||
| <?php | <?php | ||
| - | # Using $layout to define an alternate layout | + | // Using $layout to define an alternate layout |
| class RecipesController extends AppController { | class RecipesController extends AppController { | ||
| function quickSave() { | function quickSave() { | ||
| $this->layout = 'ajax'; | $this->layout = 'ajax'; | ||
| + | } | ||
| + | } | ||
| + | ?> | ||
| + | </pre> | ||
| + | <p>You can also change the title of the page (that is located in the bar at the top of your browser) using <code>$pageTitle</code>. In order for this to work properly, your layout needs to include the <code>$title_for_layout</code> variable, at least between the <code><title></code> and <code></title></code> tags in the head of the HTML document. | ||
| + | </p> | ||
| + | <pre> | ||
| + | <?php | ||
| + | // Using $pageTitle to define the page title | ||
| + | class RecipesController extends AppController { | ||
| + | function quickSave() { | ||
| + | $this->pageTitle = 'My search engine optimized title'; | ||
| } | } | ||
| } | } | ||
| ?> | ?> | ||
| </pre> | </pre> | ||
| <p> | <p> | ||
| - | The $pageTitle controller attribute allows you to set the title of the rendered page. In order for this to work properly, your layout needs to include the $title_for_layout variable, at least between <title&gt; tags in the head of the HTML document. | + | You can also set the page title from the view using <code>$this->pageTitle</code> (You must include the <code>$this-></code> part.) This is recommended, as it better separates the logic from the layout and content. For a static page you must use <code>$this->pageTitle</code> in the view if you want a custom title. |
| </p> | </p> | ||
| <p> | <p> | ||
| - | Just set $pageTitle to a string you’d like to see in the &lt;title&gt; of your document. | + | If <code>$this->pageTitle</code> 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. |
| </p> | </p> | ||
