Andrew
Andrew

Reputation: 238777

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?

Upvotes: 8

Views: 16077

Answers (4)

Emanuele
Emanuele

Reputation:

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());

Upvotes: 0

Jack Sleight
Jack Sleight

Reputation: 17118

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

Upvotes: 2

Aron Rotteveel
Aron Rotteveel

Reputation: 83173

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

Upvotes: 21

gmcrist
gmcrist

Reputation: 178

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

Upvotes: 6

Related Questions