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

发布于 2024-08-21 16:26:40 字数 649 浏览 12 评论 0原文

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

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

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:

-index.phtml  
-table.phtml

And dir2/views/scripts:

-table.phtml

Now, it will render the index.phtml in dir1 since dir 2 doesn't have an index.phtml.

Index.phtml looks something like:

<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.
Is there a simple solution/explanation to my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-08-28 16:26:40

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

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

Seems that paths are used in LIFO order.

Take a look at viewRednderer and view source files to see how does it work.

可是我不能没有你 2024-08-28 16:26:40

你可以使用

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

更具体的

u can use

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

that is more specific

相思碎 2024-08-28 16:26:40

我最终扩展了 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);
                }
            }
        }
    }
}

I ended up extending Zend_View and adding the function 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);
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文