在 Zend HeadScript 视图助手中修改堆栈

2024-01-26

我正在尝试攻击这个问题 https://stackoverflow.com/questions/2253170/zend-framework-last-code-to-execute-before-layout-is-rendered从完全不同的角度来看,因为看起来我无法通过这种方式实现我的目标。

我想循环遍历项目堆栈HeadScript 视图助手 http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headscript,并对其进行修改。文档 http://framework.zend.com/manual/en/zend.view.helpers.html#zend.view.helpers.initial.headscript为此和其他一些视图助手做出了这样的声明:

HeadScript 重写了每个append(), offsetSet()、prepend() 和 set() 为 强制使用特殊方法 如上所列。在内部,它存储 每个项目作为一个 stdClass 令牌,其中 它稍后使用序列化 itemToString() 方法。这允许 您对以下项目进行检查 堆栈,并可选择修改这些 通过简单地修改对象来修改项目 回。

那么,这个“返回的对象”在哪里呢?我在这里遗漏了一块拼图。

感谢您的帮助!


In the toString()的方法Zend_View_Helper_HeadScript我注意到一个foreach()循环播放$this,所以我尝试了一下并且成功了。这是我编写的 HeadScript 扩展,它说明了解决方案:

class My_View_Helper_HeadScript extends Zend_View_Helper_HeadScript
{
    public function toString($indent = null)
    {
        $files = array();
        foreach ($this as $key => $item) {
            if (!empty($item->attributes)
                    && array_key_exists('src', $item->attributes)
                    && ('scripts' == substr($item->attributes['src'], 1, 7))) {
                $files[] = $item->attributes['src'];
                unset($this[$key]);
            }
        }
        if (0 < count($files)) {
            $this->prependFile('/combo.php?type=scripts&files=' . implode(',', $files));
        }
        return parent::toString($indent);
    }
}

In Bootstrap.php以下几行指向我的助手:

$this->bootstrap('view');
$view = $this->getResource('view');
$view->addHelperPath('My/View/Helper', 'My_View_Helper');

在我的布局中,我有这一行:

<?php echo $this->headScript(); ?>

如果我的解决方案有任何不清楚之处,请告诉我,我会更新以澄清。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Zend HeadScript 视图助手中修改堆栈 的相关文章

随机推荐