try this (comments show the changes):
[EDIT]
ok, I think I got it. In order to use this helper to fill the headscript()
stack we have to register the results with the view renderer.
public function loadJs() {
$dir = '../public/js';
$dir = scandir($dir);
//get bootstrap
$bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
//get current view renderer
$view = $bootstrap->getResource('view');
$jsFiles = array();
foreach ($dir as $key => $value) {
if (!is_dir($value))
$jsFiles[] = $value;
}
if (is_array($jsFiles)) {
foreach ($jsFiles as $key => $val) {
if (strpos($val, '.js') != FALSE) {
//assign file to headscript stack in the cuurent view renderer
$view->headScript()->appendFile($this->view->baseUrl('js/' . $val)). "\n";
}
}
}
}
now when we echo the headscript()
in the layout these files will be rendered, once.
Just for info I do init()
my view in the bootstrap:
//truncated because it's too long...
protected function _initView()
{
//Initialize view
$view = new Zend_View();
//set doctype for default layout
$view->doctype(Zend_Registry::get('config')->resources->view->doctype);
//set css includes
$view->headLink()->setStylesheet('/css/normalize.css');
//add javascript files
$view->headScript()->setFile('/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;
}
Then I used the helper in the layout like:
<?php echo $this->doctype() . "\n" ?>
<?php $this->LoadJs() ?>
<html>
<head>
<?php echo $this->headTitle() . "\n"; ?>
<?php echo $this->headMeta() . "\n"; ?>
<?php echo $this->headLink() . "\n" ?>
<?php echo $this->headscript() . "\n" ?>
<!--[if IE]><script type="text/javascript" src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>
doing it like this, LoadJs()
doesn't render anything directly, instead the method just dumps files onto the headscript()
placeholder.
My results:
<script type="text/javascript" src="/javascript/modernizr.js"></script> <!--added by bootstrap -->
<script type="text/javascript" src="/javascript/mediaelement/build/jquery.js"></script> <!-- added by bootstrap -->
<script type="text/javascript" src="http://schoenwolf.local/javascript/modernizr.js"> </script><!-- added by LoadJs -->
I hope it works for you as well.