修改Zend HeadScript View Helper中的堆栈

时间:2010-02-15 22:15:51

标签: php zend-framework zend-view

我试图从一个完全不同的角度攻击this problem,因为看起来我不能达到我的目标。

我想循环遍历HeadScript View Helper中的项目堆栈,并对其进行修改。 The documentation为此以及其他一些视图帮助者发表此声明:

  

HeadScript会覆盖每个append(),   offsetSet(),prepend()和set()to   强制使用特殊方法   如上所列。在内部,它存储   每个项目作为stdClass标记,其中   它后来使用。序列化   itemToString()方法。这允许   你要对物品进行检查   堆栈,并可选择修改这些   简单地修改对象的项目   返回。

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:4)

toString()的{​​{1}}方法中,我注意到Zend_View_Helper_HeadScript上的foreach()循环,所以我尝试了它并且它有效。这是我写的一个HeadScript扩展,它说明了解决方案:

$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');

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

相关问题