2

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!

Community
  • 1
  • 1
Sonny
  • 8,204
  • 7
  • 63
  • 134

1 Answers1

4

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.

Sonny
  • 8,204
  • 7
  • 63
  • 134
  • how do you use it ? i've trying to get this to work http://blog.hines57.com/2011/03/13/zendframework-minify/ by simply having to change one like in the layout echo $this->miniHeadScript() – max4ever May 03 '11 at 14:29
  • @max4ever - I have added more details to the solution. It overloads the existing `$this->headScript()` call. Changing to `miniHeadScript()` is an additional step. – Sonny May 03 '11 at 15:03
  • i don't know why but the foreach ($this ...) always skips on my configuration, i found the solution to be, to iterate over $this->view->headLink(); i posted some comments on the blog http://blog.hines57.com/2011/03/13/zendframework-minify/comment-page-1/#comment-353 – max4ever May 03 '11 at 15:59
  • @max4ever - It's because you changed the name of the view helper – Sonny May 03 '11 at 19:19
  • ehm, sorry what do you mean ? – max4ever May 04 '11 at 08:26
  • I left a comment on your blog. – Sonny May 04 '11 at 13:23
  • it won't work for some reason, nevermind i'll just use my "hack" – max4ever May 04 '11 at 13:47