在CakePHP的布局我为了设置页面的标题像这样取块在cakePHP中设置为控制器名称的标题块的默认值?
<title>Example - <?php echo $this->fetch('title');?></title>
我已经注意到了,除非我创建一个视图块“标题”或某个值分配给块,标题的值总是对应于控制器的名称。我还没有在任何地方发现这种行为。有什么方法可以改变它吗?我的CakePHP版本是2.7.5。
在CakePHP的布局我为了设置页面的标题像这样取块在cakePHP中设置为控制器名称的标题块的默认值?
<title>Example - <?php echo $this->fetch('title');?></title>
我已经注意到了,除非我创建一个视图块“标题”或某个值分配给块,标题的值总是对应于控制器的名称。我还没有在任何地方发现这种行为。有什么方法可以改变它吗?我的CakePHP版本是2.7.5。
你可以传递一个默认值由$this->fetch()
是输出一个值尚未使用第2参数分配: -
echo $this->fetch('title', 'Default title');
要覆盖默认只使用$this->assign()
在您的视图($this->fetch()
前叫): -
$this->assign('title', 'Overridden title');
谢谢..它似乎虽然$ this-> fetch('title')_does_返回一个默认值,相应的控制器的名称..因此,默认值是无用的..为什么$ this-> fetch ('title')返回一个默认值?你能证实这种行为吗? –
在你的控制器
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');
}
}
在你的布局或视图
cakephp 2.x
<?php echo $this->fetch('title'); ?>
cakephp 3.x
<?= $this->fetch('title'); ?>
可以是您使用该蛋糕的版本,你告诉我? – Fury