phpzend-frameworkzend-viewzend-layout

Cannot echo headTitle in layout view


I got my headTitle up running, except i cannot echo it in my layout file as i need it for page header.

This is how i done it:

Bootstrap.php:

protected function _initDefaultHelpers() {
    $this->bootstrap('view');
    $view = $this->getResource('view');

    $view->headTitle('Awesome Website');
    $view->headTitle()->setSeparator(' - ');

My Controller:

public function indexAction() {
    $this->_helper->layout()->getView()->headTitle('IndexPage');        
}

When i open index i get: Awesome Website - IndexPage, which is perfect.

But in my master.phtml where i use:

<?php echo $this->headTitle(); ?>

Gives absolutely nothing. At this point i only want the title "IndexPage" and not the entire title, so this also has to be considered.

Thanks in advance.


Solution

  • this work: tested by me locally after creating a new project with zend tool!

    //application.ini

    [production]
    phpSettings.display_startup_errors = 0
    phpSettings.display_errors = 0
    includePaths.library = APPLICATION_PATH "/../library"
    bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
    bootstrap.class = "Bootstrap"
    appnamespace = "Foo"
    resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
    resources.frontController.params.displayExceptions = 0
    
    ; layout stuff
    resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
    
    ; view stuff
    resources.view[] = ""
    
    [staging : production]
    
    [testing : production]
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    
    [development : production]
    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1
    resources.frontController.params.displayExceptions = 1
    

    //into Bootstrap.php

    protected function _initDefaultHelpers() {
        $this->bootstrap('view');
        $view = $this->getResource('view');
    
        $view->headTitle('Foo');
        $view->headTitle()->setSeparator(' :: ');
        $view->doctype("XHTML1_STRICT");
    }
    

    //into layout.phtml

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="it" xml:lang="it">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    
        <?=$this->headTitle()?>
    
    </head>
    <body>
        <?=$this->pageTitle?>
        <?=$this->layout()->content?>
    
    </body>
    </html>
    

    //into view

    <? $this->pageTitle("Bar"); ?>
    

    //create view/helper/PageTitle.php

    <?
    class Zend_View_Helper_PageTitle extends Zend_View_Helper_Abstract
    {
        public function pageTitle($title)
        {
            $this->view->headTitle($title);
            $this->view->pageTitle = '<h1>' . $title . '</h1>';
        }
    }
    

    After that the name of your page will be: Foo :: Bar