在布局视图中无法回显headTitle

时间:2011-11-09 13:45:27

标签: php zend-framework zend-view zend-layout

我的头文件正在运行,除了我无法在我的布局文件中回显它,因为我需要它用于页面标题。

我就是这样做的:

bootstrap.php中:

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

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

我的控制器:

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

当我打开索引时,我得到:真棒网站 - 索引页面,这是完美的。

但是在我使用的master.phtml中:

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

绝对没有任何意义。此时我只想要标题“IndexPage”而不是整个标题,所以这也需要考虑。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这项工作:在使用zend工具创建新项目后由我本地测试!

//的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

//进入B​​ootstrap.php

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

    $view->headTitle('Foo');
    $view->headTitle()->setSeparator(' :: ');
    $view->doctype("XHTML1_STRICT");
}

//进入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>

//进入视图

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

//创建视图/帮助器/ 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>';
    }
}

之后,您的网页名称将为: Foo :: Bar