1

是否可以拥有多个

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

在一个视图中?

喜欢

<?php $this->headScript()->appendFile('foo.js'); ?>
<?php echo $this->headScript(); ?>

some other html here

<?php $this->headScript()->appendFile('bar.js'); ?>
<?php echo $this->headScript(); ?>

目前它重复foo.js了,那么有没有办法清洁headScript容器?

更新

确切的问题是我对工作方式不满意<?php $this->headScript()->captureStart(); ?>。因为我不能<script type="...">在那里指定,所以我的 IDE 不会将和之间的代码captureStart视为captureEndjavascript。

所以我想将输出分成两部分,<script type="text/javascript">在它们之间

PS:我知道最好将js移动到单独的文件中,但是在这个特殊的地方我需要它被内联指定

4

3 回答 3

2

可能是我错过了smth,为什么你不能使用setFileappendFile

于 2012-04-24T12:51:50.633 回答
1

问题是多个 .js 部分的分离。这是完全可行的,因为 headlink、headscript 等的 viewhelper 实现了 ArrayAccess 接口。

这就是我的做法 - 使用 ZF2 Bootstrap(来自 Skeleton 以保持一致):

<!-- allows for comments as well, within diff. .js script tag outputs -->
<?php
$this->headScript()
    ->prependFile($this->basePath() . '/js/bootstrap.min.js')
    ->prependFile($this->basePath() . '/js/jquery.min.js')
    ->prependFile($this->basePath() . '/js/respond.min.js', 'text/javascript', array('conditional' => 'lt IE 9',))
    ->prependFile($this->basePath() . '/js/html5shiv.js',   'text/javascript', array('conditional' => 'lt IE 9',));

// Notice! below we'll echo out what we have in the headScript placeholder object
echo $this->headScript();

// Now, since it implements ArrayAccess interface, we can use exchangeArray() method
// to clear out (if you will) the stored array of .js files we've previously assigned
$this->headScript()->exchangeArray(array());
?>

<!-- Some other js file(s) I have to include -->
<?php 
$this->headScript()
     ->appendFile($this->basePath() . '/js/scripts.js', 'text/javascript');

// same as above for consistency
echo $this->headScript();
$this->headScript()->exchangeArray(array());
?>

这应该有很大帮助。

于 2013-11-14T14:50:46.813 回答
0

The way this usually works is <?php echo $this->headScript(); ?> is in your layout. It will echo out all the scripts you assign it by calling headScript() once. I usually have a few scripts in my Boostrap, like jquery or modernizer.

//BootStrap.php
protected function _initView() {
        //Initialize view
        $view = new Zend_View();

        $view->doctype(Zend_Registry::get('config')->resources->view->doctype);

        $view->headMeta()->appendHttpEquiv('Content-Type', Zend_Registry::get(
                        'config')->resources->view->contentType);
        $view->headLink()->setStylesheet('/css/normalize.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/liquid.css');
        $view->headLink()->appendStylesheet('/css/blueprint/src/typography.css');
        $view->headLink()->appendStylesheet(
                '/javascript/mediaelement/build/mediaelementplayer.css');
        $view->headLink()->appendStylesheet('/css/main.css');
        $view->headLink()->appendStylesheet('/css/nav.css');
        $view->headLink()->appendStylesheet('/css/table.css');

        //add javascript files
        $view->headScript()->setFile('/javascript/mediaelement/build/jquery.js');
        $view->headScript()->appendFile('/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;
    }

If I need to add scripts later it's just a matter of passing them in the controller usually in preDispatch() :

public function preDispatch() {

        if ($this->getRequest()->getActionName() == 'play') {
            $this->_helper->layout->setLayout('play');
            $this->view->headScript()->appendFile(
                    'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js'
                    );
            $this->view->headScript()->appendFile(
                    '/javascript/mediaplayer/jwplayer.js'
                    );
        }
    }

One call to <?php echo $this->headScript(); ?> will echo out all 4 of these script files.
The same kind of thing can be done with inline scripts using the inlineScript() helper. The inlineScript() helper is the one you use if you need javascript somewhere other then the head of you file.

于 2012-04-24T04:49:36.193 回答