【问题标题】:view render in zend not working properly as expected在 zend 中查看渲染无法按预期正常工作
【发布时间】:2012-05-02 03:11:35
【问题描述】:

我的 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 设置为“默认”。

【问题讨论】:

  • 我不太确定您要做什么。 render() 方法应该返回渲染的 HTML 而不是显示它。如果您正在寻找一种在 Zend 中布局模板的方法,您可能想看看 Zend_Layout 以了解如何正确地做到这一点。 framework.zend.com/manual/en/zend.layout.html

标签: zend-framework


【解决方案1】:

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

如果您使用 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"

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

【讨论】:

    【解决方案2】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2021-05-04
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多