Zend: How to add webpage title in all my views?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 10:04:52

问题


For now I have to add title in all my views separately like this:

<head>
       <title>TestProject - Home</title>
</head>

and

<head>
       <title>TestProject - Dashboard</title>
</head>

Now if I want to change TestProject part of title then I have to change it in all my views. How can I mentioned this in BootStrap.php and add it in all views? And whenever I have to change this, I will change this in one place.


回答1:


You should look into the headTitle view helper. You can put this snippet below in your bootstrap file (from the documentation at http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headtitle).

// setting the controller and action name as title segments:
$request = Zend_Controller_Front::getInstance()->getRequest();
$this->headTitle($request->getActionName())
     ->headTitle($request->getControllerName());

// setting the site in the title; possibly in the layout script:
$this->headTitle('Test Project');

// setting a separator string for segments:
$this->headTitle()->setSeparator(' / ');

Then you can set each page title individually in controller like this:

$this->view->headTitle('The page name')

The rendered title will look like this:

<title>Test Project / The page name</title>

Oh, and you need this in your layout script where the tag would go:

<?php echo $this->headTitle() ?>



回答2:


Look into using layouts and placeholders. Layouts are applied to all your views, you can set the title there. In your controller you can then set the "home" or "dashboard" part to a placeholder that will be used by your layout.




回答3:


In Bootstrap.php

protected function _initViewHelpers() {
    $view = new Zend_View();
    $view->headTitle('Main Title')->setSeparator(' - ');
}

In any view/.phtml

<?php 
    $this->headTitle()->prepend('Page Title');
    echo $this->headTitle();
?>



回答4:


This is best way to set page title into the controller. If you want to set new title then use..

public function init()
 {
    $this->view->headTitle('My title'); 
 }

If you want to prepend the title with existing title then use

$this->view->headTitle()->setSeparator(' - ')->prepend('Manager'); 



回答5:


I think you may do this with js as you just site the title =TestProject only then in each page you could with javascript read the title then concatenate the extra title

this if you are using something like master pages



来源:https://stackoverflow.com/questions/6002123/zend-how-to-add-webpage-title-in-all-my-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!