Zend framework zend中的视图渲染未按预期正常工作

Zend framework zend中的视图渲染未按预期正常工作,zend-framework,Zend Framework,我的zend布局和脚本都很好。但是当我在IndexController的init函数中写入$this->view->render(“header.phtml”)时,它不会在屏幕上显示任何内容,而当我写入echo($this->view->render(“header.phtml”);它会显示我的header.phtml文件。这是我的IndexController 类IndexController扩展Zend\u控制器\u操作 { 变量$crt\U theme被设置为“默认值”。render()正

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

类IndexController扩展Zend\u控制器\u操作 {

变量$crt\U theme被设置为“默认值”。

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

但是,直接在控制器中渲染是非常罕见的。至少,您应该从
视图/scripts/index/index.phtml
渲染页眉和页脚,尽管使用会更好。如果您使用
Zend_应用程序
,那么您可以通过添加以下内容开始使用
Zend_布局

<?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>
到您的
应用程序/config/application.ini
文件。然后,您需要创建一个
应用程序/layouts/scripts/layout.phtml
文件,该文件可能如下所示:

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

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

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

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

如果需要更改默认布局的名称,请使用脚本名称添加这一行,而不使用.phtml

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"
使用此文件的方法尽可能多,但下面是我如何使用它的示例。
我喜欢在application.ini文件中设置我的项目默认值,这样如果我需要更改任何内容都很容易

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;
    }
然后在我的引导中,我设置了我想要使用的视图,我在这里这样做,这样如果我有多个布局(我通常这样做),就可以很容易地在一个地方更改css或js文件

<?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>
请注意所有这些headLink()、headScript()和docType()条目,它们是设置数据的位置,将在布局中使用

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

$this->_helper->layout->setLayout('myOtherLayout');
这样做将更改控制器中每个操作的布局。为了更具选择性,您可以使用条件,如:


我真的不确定您想做什么。
render()
方法应该返回呈现的HTML,而不是显示它。如果您正在寻找一种在Zend中布局模板的方法,您可能想看看Zend_layout,了解如何正确地执行此操作。
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;
    }
<?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>
$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'
                    );
        }