繁体   English   中英

如何从Controller(Zend Framework)控制布局中加载的js文件

[英]How to control the js files that are load in layout, from the Controller (Zend Framework)

首先,我是Zend Framework的初学者,也许有一个简单的方法可以做到这一点,但我不知道。

我要实现的目标是根据所使用的Controller加载不同的js文件(在布局头中)。 我无法将js文件作为变量添加到Layout对象,因为在那些Controller中禁用了布局。

正确的做法是什么? 谢谢。

编辑(代码)

在CalendarController中(使用Subdigger的方法):

    public function init()
    {
      $this->_helper->layout->disableLayout();

      $js = new Application_View_Helper_Javascript();
      //get an array with the basename of the js files          
      $jsFiles = $js->addFiles('calendar');

      foreach ($jsFiles as $k=>$file){
           $this->view->headScript()->appendFile('/js/' . $file.'.js');
      }


    }

并在layout.phtml中:

<?php
 echo $this->doctype()."\n";
 ?>
 <html>
    <head>
       <?php
         echo $this->headMeta()."\n";
         echo $this->headLink()."\n";
         echo $this->headTitle()."\n";
         echo $this->headScript()."\n";

你可以这样:在控制器中

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        $this->view->headScript()->appendFile(
          '/js/prototype.js',
          'text/javascript',
          array('conditional' => 'lt IE 7')
        );
    }
......
}

然后来看:

.....
  <head>
    <?php echo $this->headScript() ?>
  </head>
.....

这个

我了解您的问题,但未正确理解您的意图。 您在注释中说,已在indexController中启用了布局,但其他人禁用了布局。 如果您调度到另一个控制器并禁用布局,则不应使用layout.phtml。 那么,您如何使用它呢? 您是否在某个地方包含了?

我认为这里的解决方案不是禁用布局,而是在那些“其他”控制器中加载其他布局。

$this->_helper->layout->setLayout('foobaz');

您可以为每个控制器甚至是控制器中的动作加载不同的布局。 我注意到的另一件事是,您将其加载到init()方法中。 我在动作或pre / postDispatch方法中执行所有这些逻辑。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM