简体   繁体   中英

Zend Framework: headTitle()->append() issue

Has anyone run into this problem...

In my layout.phtml I have:

<head>
    <?= $this->headTitle('Control Application - ') ?>
</head>

then in index.phtml I have:

<? $this->headTitle()->append('Client List'); ?>

I expect that, when I go to my index action, the title should be 'Control Application - Client List' but instead I have 'Client ListControl Application - '

What is going on? How can I fix this?

Default behaviour of the headTitle() is to append to the stack. Before calling headTitle() in layout.phtml, your stack is:

Clientlist

Then, you call headTitle with the first argument and no second argument ( which makes it default to APPEND ), resulting in the following stack:

ClientListControl Application -

The solution, in layout.phtml:

<?php 
    $this->headTitle()->prepend('Control Application -');
    echo $this->headTitle();
?>

Additionally, you can use the setPrefix method in your layout as such:

<head>
    <?= $this->headTitle()->setPrefix('Control Application') ?>
</head>

And in your controllers/actions/etc use the standard append/prepend:

<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->append('Client List');
?>

I don't actually use headTitle, but do use ZF, and I had a quick look on the mailing list, this might solve the problem:

<head>
    <?= $this->headTitle('Control Application') ?>
</head>

Then:

<?php
$this->headTitle()->setSeparator(' - ');
$this->headTitle()->prepend('Client List');
?>

This happens because the layout is the last script to be executed. So you actually do the append BEFORE the set of the title, so that there's nothing to append to yet. Set the main title (Control Application) in a Controller. For example I always do it in the predispatch action of a initPlugin so that it is execute before any other Controller Action, and I can append or prepend at will.

To use such a plugin just define a new Class extending Zend_Controller_Plugin_Abstract and define a function preDispatch( Zend_Controller_Request_Abstract $request) where you can put all your common-to-the-whole-site code, and to register the plugin just put it into the controllerFront of your bootstrap: $controller->registerPlugin(new InitPlugin());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM