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

时间:2012-05-02 03:11:35

标签: zend-framework

我的zend布局和脚本检测正常。但是在我的IndexController的init函数中,我写了$ this-> view-> render(“header.phtml”)它没有显示任何一个屏幕,而当我写回声时($ this-> view-&gt ; 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";
}

}

当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);

变量$ crt_theme设置为'default'。

2 个答案:

答案 0 :(得分:6)

Rob'的答案是正确的,尽管您可能需要更多信息,因为您似乎正在努力使其变得复杂。
ZF用作MVC时可以放置布局和默认使用它们。 BR />

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

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

并在该路径中添加文件layout.phtml
如果您需要更改默认布局的名称,请在没有.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"

然后在我的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;
    }

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

现在布局,其他基于操作的脚本的实际内容通常由占位符$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'
                    );
        }

答案 1 :(得分: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>