2

我试图从一个完全不同的角度来解决这个问题,因为看起来我无法以这种方式实现我的目标。

我想遍历HeadScript View Helper中的项目堆栈,并对其进行修改。 这个和其他一些视图助手的文档做出了这样的声明:

HeadScript 覆盖 append()、offsetSet()、prepend() 和 set() 中的每一个,以强制使用上面列出的特殊方法。在内部,它将每个项目存储为 stdClass 标记,稍后使用 itemToString() 方法对其进行序列化。这允许您对堆栈中的项目执行检查,并可选择通过简单地修改返回的对象来修改这些项目。

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

谢谢你的帮助!

4

1 回答 1

4

在我注意到一个循环的toString()方法中,所以我尝试了它并且它有效。这是我编写的一个 HeadScript 扩展,它说明了解决方案:Zend_View_Helper_HeadScriptforeach()$this

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);
    }
}

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

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

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

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

如果我的解决方案在任何方面都不清楚,请告诉我,我会更新它以澄清。

于 2010-02-15T23:25:27.930 回答