1

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

$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,但事实并非如此。
我的问题有简单的解决方案/解释吗?

4

3 回答 3

1

似乎路径以 LIFO 顺序使用。

查看源文件以了解它是如何工作的viewRedndererview

于 2010-02-11T09:37:45.787 回答
0

你可以用

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

那更具体

于 2010-06-13T09:16:38.020 回答
0

我最终扩展了 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);
                }
            }
        }
    }
}
于 2011-06-27T12:39:19.790 回答