PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Specifying the Page Title

Often, a site will include the site or business name as part of the page title, and then add additional information based on the location within the site. As an example, the zend.com website includes the string "Zend.com" on all pages, and the prepends information based on the page: "Zend Server - Zend.com". Within Zend Framework, the headTitle() view helper can help simplify this task.

At its simplest, the headTitle() helper allows you to aggregate content for the <title> tag; when you echo it, it then assembles it based on the order in which segments are added. You can control the order using prepend() and append(), and provide a separator to use between segments using the setSeparator() method.

Typically, you should specify any segments common to all pages in your bootstrap, similar to how we define the doctype. In this case, we'll define a _initPlaceholders() method for operating on all the various placeholders, and specify an initial title as well as a separator.

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    // ...

    protected function _initPlaceholders()
    {
        $this->bootstrap('View');
        $view = $this->getResource('View');
        $view->doctype('XHTML1_STRICT');

        // Set the initial title and separator:
        $view->headTitle('My Site')
             ->setSeparator(' :: ');
    }

    // ...
}

Within a view script, we might want to add another segment:


<?php $this->headTitle()->append('Some Page'); // place after other segments ?>
<?php $this
->headTitle()->prepend('Some Page'); // place before ?>

In our layout, we will simply echo the headTitle() helper:


<?php echo $this->doctype() ?>
<html>
    <?php echo $this->headTitle() ?>
    <!-- ... -->

This will generate the following output:

<!-- If append() was used: -->
<title>My Site :: Some Page</title>

<!-- If prepend() was used: -->
<title>Some Page :: My Site</title>

Zend Framework