CakePHP:将默认字符串附加到页面标题
我在控制器的操作中设置单独的页面标题,使用:
$this->set( 'title_for_layout', 'Some Title' );
我想要实现的是在渲染之前将站点标题字符串附加到所有这些标题,即“Some Title :: Site Name” 。
最简单的方法是手动将此字符串添加到设置页面标题的每个点 - 但这就像暴力方式。
我尝试的是重写每个控制器的 beforeRender()
方法并添加以下语句:
$this->set( 'title_for_layout', $this->title_for_layout . ' » ' . Configure::read( 'Site.title' ) );
我认为这会将站点的标题附加到每个操作的页面标题 - 但我得到的是
Notice (8): Undefined variable: SomeController::title_for_layout [APP\controllers\some_controller.php, line xx]
:似乎 title_for_layout
(在控制器中的各个操作中设置)尚未设置 - 这会引发此错误。
我的问题是,在哪里&如果不是这样的话,如何将站点标题全局附加到所有页面标题?
谢谢, 米^e
I'm setting individual page titles in my controllers' actions using:
$this->set( 'title_for_layout', 'Some Title' );
What I want to achieve is append a site title string to all these titles before rendering, i.e. say, "Some Title :: Site Name".
The easiest way out would be to add this string manually to each point where the page title is set - but that's like the brute force way.
What I tried was to override the beforeRender()
method of each controller and add this statement:
$this->set( 'title_for_layout', $this->title_for_layout . ' » ' . Configure::read( 'Site.title' ) );
I thought this would append the site's title to the page title for each action - but what I get instead is:
Notice (8): Undefined variable: SomeController::title_for_layout [APP\controllers\some_controller.php, line xx]
It seems like the title_for_layout
(set in individual actions in the controller) hasn't been set yet - which is throwing up this error.
My question is, where & How can I append the site title globally to all page titles - if not in this way?
Thanks,
m^e
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
$this->pageTitle
而不是$this->title_for_layout
use
$this->pageTitle
instead of$this->title_for_layout
如果您确实想将相同字符串附加到所有页面标题,请将其放在$title_for_layout之后的布局中。
If you really want to append the same string to all your page titles, put it in the layout after $title_for_layout.