0

我的 zend 布局和脚本检测良好。但是在我的 IndexController 的 init 函数中,我写 $this->view->render("header.phtml") 它不会在屏幕上显示任何内容,而当我写 echo ($this->view->render(" header.phtml"); 它显示我的 header.phtml 文件。这是我的 IndexController

类 IndexController 扩展 Zend_Controller_Action {

public function init()
{
    $layout = $this->_helper->layout();
    //$this->view = $this->getResource('View');
    echo ($this->view->render("header.phtml"));
        //echo"<pre>";
        //var_dump($this->view);
        //var_dump(APPICATION_PATH);
}

public function indexAction()
{
    // action body
    echo "In default index con";
}

}

此外,当我将我的 url 提供给/mypath/index 时,它不会显示我只是在回显的简单行“我在索引控制器中”。在我的引导程序中,这是我的 Zend_Layout 设置。

        Zend_Loader::loadClass('Zend_View');
$this->view = new Zend_View();
$this->view->setEncoding('UTF-8');
$this->view->setScriptPath($this->root .'/application/default/views/scripts');
    $viewRenderer=new Zend_Controller_Action_Helper_ViewRenderer();
     // Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
     // $view->setScriptPath($this->root.'/'.$theme.'/views/scripts/');
     //$view->setScriptPath($this->root.'/application/default/views/scripts/');
     $this->view->addScriptPath($this->root.'/application/admin/views/scripts');
     $this->view->addScriptPath($this->root.'/application/business/views/scripts');
    // $this->view->setHelperPath($this->root .'/application/default/views/helpers');
     $this->layout = Zend_Layout::startMvc(
        array(
                'layoutPath' => $this->root . '/application/default/views/'.$crt_theme.'/layouts',
                'layout' => 'layout'
                )
                );
        $this->registry->set("theme", $crt_theme);

变量 $crt_theme 设置为“默认”。

4

2 回答 2

6

Rob 的回答是正确的,尽管您可能需要更多信息,因为您似乎正在努力使这变得复杂。
ZF 在用作 MVC 时有放置布局和使用它们的默认值的地方。

如果您使用 Zend_Tool 命令行界面,请从 commmand: 开始,zf enable layout该工具会将默认目录和默认layout.phtml添加到您的项目中。
application.ini中,它将添加以下行:

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

并在该路径上添加layout.phtml
文件

resources.layout.layout = master

您可以想到使用此文件的多种方法,但这里是我如何使用它的示例。我喜欢在我的application.ini
文件 中设置我的项目默认值,因此如果我需要更改任何内容,这很容易。

View Settings
;*************
resources.view[]=
resources.view.charset = "UTF-8"
resources.view.encoding = "UTF-8"
resources.view.doctype = "HTML5"
resources.view.language = "en"
resources.view.contentType = "text/html; charset=UTF-8"

然后在我的引导程序中,我设置了我想要使用的视图,我在这里进行设置,以便如果我有多个布局(我通常这样做),很容易在一个地方更改 css 或 js 文件。

protected function _initView() {
        //Initialize view
        $view = new Zend_View();

        $view->addHelperPath('/../library/Application/View/Helper');

        $view->doctype(Zend_Registry::get('config')->resources->view->doctype);

        $view->headTitle('Our Home');

        $view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
                        'config')->resources->view->contentType);
        $view->headLink()->setStylesheet('/css/normalize.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/typography.css');
        $view->headLink()->appendStylesheet(
                '/javascript/mediaelement/build/mediaelementplayer.css');
        $view->headLink()->appendStylesheet('/css/main.css');
        $view->headLink()->appendStylesheet('/css/nav.css');
        $view->headLink()->appendStylesheet('/css/table.css');

        //add javascript files
        $view->headScript()->setFile('/javascript/mediaelement/build/jquery.js');
        $view->headScript()->appendFile('/javascript/modernizr.js');

        //add it to the view renderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                        'ViewRenderer');
        $viewRenderer->setView($view);
        //Return it, so that it can be stored by the bootstrap
        return $view;
    }

请注意所有这些 headLink()、headScript() 和 docType() 条目,这些是为占位符设置数据的位置,将在布局中使用。

现在布局,来自其他基于动作的脚本的实际内容通常由占位符呈现$this->layout()->content

<?php
echo $this->doctype() . "\n";//placeholder
?>
<html>
    <head>
        <title></title>
        <?php echo $this->headMeta() . "\n" ?><!-- displays all meta data passed -->
        <?php echo $this->headLink() . "\n" ?><!-- displays all links passed -->
        <?php echo $this->headscript(). "\n"?><!-- displays all scripts passed -->
    </head>
    <body>
        <section class="container">
            <header class="block">
                <hgroup id="header" class ="column span-24">
                    <h1>Our Home</h1>
                </hgroup>
                <nav>
                    <div id="nav" class="column span-24">
                        <?php echo $this->layout()->nav ?>&nbsp;<!-- Custom Placeholder -->
                    </div>
                </nav>
            </header>
            <section class="block">
                <div id="main" class="column span-18 border">
                    <div id="flash">
                        <?php
                        //flash messenger display location
                        if (count($this->messages) > 0) {
                            printf("<h3 id='flash'>%s</h3>", $this->messages[0]);
                        }
                        ?>
                    </div>
                    <?php echo $this->layout()->content; ?><!-- Default placeholder, where views are rendered -->
                </div>
                <aside id="sidebar" class="column span-4 last">
                    <?php echo $this->layout()->search ?><!-- Custom placeholder -->
                    <div id="subNav">
                        <?php echo $this->layout()->subNav ?>&nbsp;<!-- Custom placeholder -->
                    </div>
                    <div id="adminMenu">
                        <h4>Administration Links</h4>
                        <?php echo $this->layout()->adminMenu ?>&nbsp;<!-- Custom placeholder -->
                    </div>
                </aside>
            </section>
            <footer class="block">
                <div id="footer" class="column span-24">
                    <p>Created with <a href="http://framework.zend.com/">Zend Framework. &COPY; </a></p>
                </div>
            </footer>
        </section>
        <?php echo $this->inlineScript() ?><!-- placeholder -->
    </body>
</html>

希望这可以帮助!

[编辑] 还有一件事,这似乎总是下一个问题。“如何更改我的控制器/操作中的默认布局?”

要从控制器更改布局,您通常会使用 preDispatch() 方法进行更改,并将新布局的名称传递给布局助手。

$this->_helper->layout->setLayout('myOtherLayout');

这样做会改变控制器中每个动作的布局。为了更有选择性,您可以使用条件,例如:

if ($this->getRequest()->getActionName() == 'player') {
            $this->_helper->layout->setLayout('player');//reset layout
            //add 2 new headscripts
            $this->view->headScript()->appendFile(
                    'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
                    );
            $this->view->headScript()->appendFile(
                    '/javascript/mediaplayer/jwplayer.js'
                    );
        }
于 2012-05-02T05:21:32.630 回答
1

render()正在按预期工作;它返回一个字符串,你应该然后echo

然而,直接在控制器中渲染是非常不寻常的。至少,您应该从 渲染您的页眉和页脚views/scripts/index/index.phtml,尽管使用Zend_Layout会更好。如果您正在使用Zend_Application,则Zend_Layout只需添加以下内容即可开始使用:

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"

到你的application/config/application.ini文件。然后,您需要创建一个application/layouts/scripts/layout.phtml看起来像这样的文件:

<?php
$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$this->headTitle()->setSeparator(' - ');
$this->headTitle('My website');
?>
<!DOCTYPE html>
<html> 
<head>
    <?php echo $this->headMeta(); ?> 
    <?php echo $this->headTitle(); ?>
    <!-- Other <head> elements and view helpers here -->
</head>
<body>
<div id="content">
    <?php echo $this->layout()->content; ?>
</div>
</body>
</html>
于 2012-05-02T05:02:50.033 回答