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.
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() ?>
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.
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();
?>
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');
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