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

徘徊边缘 提交于 2019-12-05 18:54:57

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

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