The way this usually works is <?php echo $this->headScript(); ?>
is in your layout. It will echo out all the scripts you assign it by calling headScript() once. I usually have a few scripts in my Boostrap, like jquery or modernizer.
//BootStrap.php
protected function _initView() {
//Initialize view
$view = new Zend_View();
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
$view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
'config')->resources->view->contentType);
$view->headLink()->setStylesheet('/css/normalize.css');
$view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css');
$view->headLink()->appendStylesheet('/css/blueprint/src/typography.css');
$view->headLink()->appendStylesheet(
'/javascript/mediaelement/build/mediaelementplayer.css');
$view->headLink()->appendStylesheet('/css/main.css');
$view->headLink()->appendStylesheet('/css/nav.css');
$view->headLink()->appendStylesheet('/css/table.css');
//add javascript files
$view->headScript()->setFile('/javascript/mediaelement/build/jquery.js');
$view->headScript()->appendFile('/javascript/modernizr.js');
//add it to the view renderer
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
'ViewRenderer');
$viewRenderer->setView($view);
//Return it, so that it can be stored by the bootstrap
return $view;
}
If I need to add scripts later it's just a matter of passing them in the controller usually in preDispatch() :
public function preDispatch() {
if ($this->getRequest()->getActionName() == 'play') {
$this->_helper->layout->setLayout('play');
$this->view->headScript()->appendFile(
'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
);
$this->view->headScript()->appendFile(
'/javascript/mediaplayer/jwplayer.js'
);
}
}
One call to <?php echo $this->headScript(); ?>
will echo out all 4 of these script files.
The same kind of thing can be done with inline scripts using the inlineScript() helper. The inlineScript() helper is the one you use if you need javascript somewhere other then the head of you file.