视图渲染在zend中没有按预期正常工作

Nar*_*jis 0 zend-framework

我的zend布局和脚本检测正常.但是在我的IndexController的init函数中,我写了$ this-> view-> render("header.phtml")它没有显示屏幕上的任何内容,而当我写回信时($ this-> view-> render(" header.phtml");它显示我的header.phtml文件.这是我的IndexController

class 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";
}
Run Code Online (Sandbox Code Playgroud)

}

另外,当Igive我的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);
Run Code Online (Sandbox Code Playgroud)

变量$ crt_theme设置为'default'.

Roc*_*ord 6

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

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

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
Run Code Online (Sandbox Code Playgroud)

并在该路径上将添加文件layout.phtml
如果您需要更改默认布局的名称,请使用不带.phtml的脚本名称添加此行

resources.layout.layout = master
Run Code Online (Sandbox Code Playgroud)

您可以想到使用此文件的方法有很多种,但这里有一个我如何使用它的示例.
我喜欢在我的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"
Run Code Online (Sandbox Code Playgroud)

然后在我的bootstrap中我设置了我想要使用的视图,我在这里做,所以如果我有多个布局(我通常这样做),很容易在一个地方更改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;
    }
Run Code Online (Sandbox Code Playgroud)

请注意所有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>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!

[编辑]还有 一件事,这似乎是下一个问题."如何在控制器/操作中更改默认布局?"

要通过preDispatch()方法更改控制器的布局,只需将新布局的名称传递给布局助手即可.

$this->_helper->layout->setLayout('myOtherLayout');
Run Code Online (Sandbox Code Playgroud)

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

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'
                    );
        }
Run Code Online (Sandbox Code Playgroud)