代码之家  ›  专栏  ›  技术社区  ›  Billy ONeal IS4

Zend_框架-如何在布局中放置javascript?

  •  0
  • Billy ONeal IS4  · 技术社区  · 15 年前

    我需要一种将特定于控制器/操作的javascript插入布局的方法。Javascript需要进入 <head> 文档,远离常规内容的放置位置。我已经有了一个允许每页使用多个视图的基础设施,我已经拥有的Zend_布局充分利用了这一点:

    <?php
         $script = $this->layout()->script;
         if (!is_null($script)) : ?>
    <script type="text/javascript"> // <![CDATA[
      <?php echo $script; ?>
    // ]]>
    </script>
    <?php endif; ?>
    

    但是,我希望自动选择脚本输出,就像将普通视图自动放入 $this->layout()->content 默认为布局。我知道这个工具是由viewrenderer类提供的。基本上,我想做的是检查 /VIEWPATH/scripts/CONTROLLER/ACTION.js.php script

    3 回复  |  直到 10 年前
        2
  •  1
  •   opHASnoNAME    15 年前

    class My_Controller_Plugin_JavaScript extends Zend_Controller_Plugin_Abstract
    

    /**
     * preDispatch
     * Check controller name, and include javaScript library
     *
     * @param Zend_Controller_Request_Abstract $request
     * @return void
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $layout = Zend_Layout::getMvcInstance();
        $view = $layout->getView();
        $controller = $request->getControllerName();
    
        $jsFile = $controller . '-lib.js';
        $jsPath = $view->baseUrl() .
                   '/js/' . $controller .
                   '/';
    
        $sPath = PUBLIC_PATH . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR;
        $sPath .= $controller . DIRECTORY_SEPARATOR . $jsFile;
    
        if (file_exists($sPath)) { // load as last js (offset 100)
            $view->headScript()->offsetSetFile(
                100,
                $jsPath . $jsFile
    
    
               );
            }
        }
    }
    

    <?= $this->headScript(); ?>
    

        3
  •  1
  •   Walery Strauch Lia    10 年前

    <?php
        $this->headScript()->appendFile($this->baseUrl("js/jquery-1.4.2.min"))
            ->appendFile($this->baseUrl("js/jquery-ui-1.8.2.custom.min"));
        $this->headScript()->appendScript("js/dummy.js");
        echo $this->headScript();
    
    ?>
    

        <?php $this->headScript()->captureStart() ?>
    // start jquery functions 
        var action = '<?php echo $this->baseUrl ?>'; 
        $('foo_form').action = action;
    // end jquery functions 
        <?php $this->headScript()->captureEnd() ?>
    

    http://framework.zend.com/manual/en/zend.view.helpers.html