dongzhuo1498 2010-02-15 22:15
浏览 20
已采纳

修改Zend HeadScript View Helper中的堆栈

I am trying to attack this problem from a completely different angle, because it doesn't look like I can achieve my goal that way.

I want to loop over the item stack in the HeadScript View Helper, and make modifications to it. The documentation for this and some of the other view helpers makes this statement:

HeadScript overrides each of append(), offsetSet(), prepend(), and set() to enforce usage of the special methods as listed above. Internally, it stores each item as a stdClass token, which it later serializes using the itemToString() method. This allows you to perform checks on the items in the stack, and optionally modify these items by simply modifying the object returned.

So, where is this "object returned"? I am missing a piece of the puzzle here.

Thanks for your help!

  • 写回答

1条回答 默认 最新

  • douqiao3930 2010-02-15 23:25
    关注

    In the toString() method of Zend_View_Helper_HeadScript I noticed a foreach() loop on $this, so I tried that and it worked. Here's a HeadScript extension I wrote that illustrates the solution:

    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 the following lines to point to my helpers:

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

    In my layout, I have this line:

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

    If my solution is unclear in any way, let me know and I'll update it to clarify.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 阿里云python代码求解
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路