2012-09-13 28 views
0

我试图添加多个js文件到布局。我想在帮手里做这个。
这里是我的帮手代码:Zend:添加多个js文件到布局

// 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)); 
     } 
    } 

} 

而且在我的布局,我有:

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

的问题是,它不添加任何js文件。
如果我把回声之前:

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

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

然后脚本将几次相同的文件。
有人可以告诉我我做错了什么?

+2

为什么不直接链接'appendFile()'方法?如果你有很多JavaScript文件需要帮助,你可以考虑创建一个聚合文件。 –

+0

我有点得到它,他可能希望能够交换JavaScript文件进出,无需编辑布局或引导。 – RockyFord

回答

0

试试这个(评论显示更改):
[编辑]
好吧,我想我得到了它。为了使用这个帮助器来填充headscript()堆栈,我们必须在视图渲染器中注册结果。

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"; 
       } 
      } 
     } 
    } 

现在当我们在这些文件将被渲染,一旦布局呼应headscript()

只是为了信息,我做init()我认为在引导

//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; 
    } 

然后我用的帮手类似的布局:

<?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> 

做这样的,LoadJs()没有按”直接渲染任何东西,而不是该方法只是将文件转储到headscript()占位符。

我的结果:

<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 --> 

我希望它为你工作为好。

+0

效果相同。我有6个js文件,并且这个脚本增加了6倍所有文件 – user1409508