Clearing Scripts Added Via InlineScript/HeadScript

June 17th, 2010

Sometimes in your Zend Framework application it is useful to remove all previously scripts appended or prepended with Zend_View_Helper_InlineScript or Zend_View_Helper_HeadScript helpers.

Here is the code that I am using:

1
2
3
4
5
$scriptHelper = $this->getActionController()->view->inlineScript(); // or $view->headScript()
$container = $scriptHelper->getContainer();
foreach ($container->getKeys() as $isKey) {
    $scriptHelper->offsetUnset($isKey);
}

I am running this code in an action controller helper, but it would work equally well in a layout script, view script or action controller.

In a layout or view script the code becomes:

1
2
3
4
5
$scriptHelper = $this->inlineScript(); // or $this->headScript()
$container = $scriptHelper->getContainer();
foreach ($container->getKeys() as $isKey) {
    $scriptHelper->offsetUnset($isKey);
}

In an action controller the code becomes:

1
2
3
4
5
$scriptHelper = $this->view->inlineScript(); // or $this->headScript()
$container = $scriptHelper->getContainer();
foreach ($container->getKeys() as $isKey) {
    $scriptHelper->offsetUnset($isKey);
}

If you find this post useful, please leave a comment to let me know.