0

Im trying to add multiple js files to layout. I want do this in helper.
Here's code of my helper:

// class Zend_View_Helper_LoadJs extends Zend_View_Helper_Abstract


public function loadJs()
{
    $dir = '../public/js';      
    $dir = scandir($dir);

    $jsFiles = array();

    foreach($dir as $key => $value)
    {
        if($value != '.' && $value != '..')
            $jsFiles[] = $value;
    }


    if(is_array($jsFiles)){
        foreach($jsFiles as $key => $val)
        {
            $this->view->headScript()->appendFile($this->view->baseUrl('js/'.$val));
        }
    }

}

And in my layout, I have:

<?php $this->loadJs(); ?>

The problem is that it doesn't add any js file.
if I put echo before:

echo $this->view->headScript()->appendFile($this->view->baseUrl('js/'.$val));

or

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

then script adds few times the same file.
Someone could tell me what am I doing wrong?

user1409508
  • 623
  • 1
  • 12
  • 38
  • 2
    Why aren't you just chaining the `appendFile()` method? If you have so many JavaScript files you need a helper, you might look into creating an aggregate file instead. – Zachary Schuessler Sep 13 '12 at 19:27
  • I kinda get it, he may want to be able to swap javascript files in and out without having to edit the layout or bootstrap. – RockyFord Sep 14 '12 at 10:39

1 Answers1

0

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.

RockyFord
  • 8,529
  • 1
  • 15
  • 21