In a cakephp layout I fetch a block in order to set the title of the page like so
<title>Example - <?php echo $this->fetch('title');?></title>
I have noticed that unless I create a block 'title' in a view or assign some value to the block, the value of title always corresponds to the name of the controller. I have not found this behaviour documented anywhere. Is there any way to change this? My cakePHP version is 2.7.5.
You can pass a default value to be output by $this->fetch()
if a value hasn't already been assigned using the second parameter:-
echo $this->fetch('title', 'Default title');
To override the default just use $this->assign()
in your view (before $this->fetch()
is called):-
$this->assign('title', 'Overridden title');
In your controller
class ExampleController extends AppController {
// Set title in default for all this controller methods
public $title = "Your title";
// Override the title by setting title in method
public function
$this->set('title', 'Your title');
}
}
In your layout or view
cakephp 2.x
<?php echo $this->fetch('title'); ?>
cakephp 3.x
<?= $this->fetch('title'); ?>