简体   繁体   English

使用Zend Framework在更高的脚本路径中渲染视图

[英]Render view in higher script path with Zend Framework

Lets assume the following code within a controller: 让我们假设控制器中的以下代码:

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

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

-index.phtml  
-table.phtml

And dir2/views/scripts: 和dir2 / views / scripts:

-table.phtml

Now, it will render the index.phtml in dir1 since dir 2 doesn't have an index.phtml. 现在,它将在目录1中呈现index.phtml,因为目录2没有index.phtml。

Index.phtml looks something like: Index.phtml看起来像:

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

This is where the confusion starts for me. 这就是让我困惑的地方。 I would expect it to render the table.phtml in the last directory added to the script path stack, but it doesn't. 我希望它能在添加到脚本路径堆栈的最后一个目录中呈现table.phtml,但事实并非如此。
Is there a simple solution/explanation to my problem? 我的问题是否有简单的解决方案/解释?

Seems that paths are used in LIFO order. 似乎路径按LIFO顺序使用。

Take a look at viewRednderer and view source files to see how does it work. 查看viewRedndererview源文件以了解其工作原理。

u can use 你可以用

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

that is more specific 那更具体

I ended up extending Zend_View and adding the function renderParent: 我最终扩展了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);
                }
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM