【问题标题】:Render view in higher script path with Zend Framework使用 Zend Framework 在更高的脚本路径中渲染视图
【发布时间】:2010-02-11 08:32:00
【问题描述】:

让我们假设控制器中的代码如下:

$this->view->addScriptPath('dir1/views/scripts');
$this->view->addScriptPath('dir2/views/scripts');
$this->render('index.phtml');

其中 dir1/views/scripts 包含 2 个文件:

-index.phtml  
-table.phtml

还有 dir2/views/scripts:

-table.phtml

现在,它将在 dir1 中呈现 index.phtml,因为 dir 2 没有 index.phtml。

Index.phtml 看起来像:

<somehtml>
       <?= $this->render('table.phtml') ?>
</somehtml>

这就是我开始困惑的地方。我希望它能够呈现添加到脚本路径堆栈的最后一个目录中的 table.phtml,但事实并非如此。
我的问题有简单的解决方案/解释吗?

【问题讨论】:

    标签: zend-framework zend-view


    【解决方案1】:

    似乎路径是按 LIFO 顺序使用的。

    查看viewRedndererview 源文件以了解其工作原理。

    【讨论】:

      【解决方案2】:

      你可以使用

      > $this->view->setBasePath("../application/dir1/views");
      

      更具体的

      【讨论】:

        【解决方案3】:

        我最终扩展了 Zend_View 并添加了函数 renderParent:

        class My_View extends Zend_View
        {
            private $_file = null;
        
            private $_name = null;
        
            /**
             * Finds a view script from the available directories.
             *
             * @param $name string The base name of the script.
             * @return void
             */
            protected function _script($name)
            {
                $this->_file = parent::_script($name);
                $this->_name = $name;
        
                return $this->_file;
            }
        
            /**
             * Renders the parent script by looping through all the script paths.
             *
             * @return void
             */
            public function renderParent()
            {
                $scriptPaths = $this->getScriptPaths();
        
                $found = false;
                for ($i = 0; $i < count($scriptPaths); $i++) {
                    if ($this->_file == $scriptPaths[$i] . $this->_name) {
                        $found = true;
                    } elseif ($found) {
                        if (is_readable($scriptPaths[$i] . $this->_name)) {
                            return $this->_run($scriptPaths[$i] . $this->_name);
                        }
                    }
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-22
          • 2011-11-27
          • 1970-01-01
          • 2013-01-10
          • 2012-04-08
          • 2011-04-23
          • 1970-01-01
          • 2016-02-09
          相关资源
          最近更新 更多