1

Is it possible to have multiple

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

in one view?

Like

<?php $this->headScript()->appendFile('foo.js'); ?> <?php echo $this->headScript(); ?> some other html here <?php $this->headScript()->appendFile('bar.js'); ?> <?php echo $this->headScript(); ?> 

Currently it duplicates foo.js, so is there a way to clean the headScript container?

UPD:

The exact problem is that I'm not satisfied with how <?php $this->headScript()->captureStart(); ?> works. Because I cannot specify <script type="..."> there thus my IDE doesn't treat the code between captureStart and captureEnd as a javascript.

So I want to split output into 2 parts, with <script type="text/javascript"> between them

PS: I know that it is better to move js to a separate file, but in this particular place I need it to be specified inline

3
  • Best guess...Type the javascript then add the php around it. Javascript with these headScript and inlineScript helpers tends to be just php strings in the end... Use another IDE ??? I got nothin' else sorry. Commented Apr 24, 2012 at 5:11
  • @RockyFord: wondering what IDE could determine such stuff. Commented Apr 24, 2012 at 5:55
  • You can do this, see my detailed comment below. Commented Nov 14, 2013 at 14:51

3 Answers 3

2

May be I'm missing smth, why you can't use setFile instead appendFile ?

Sign up to request clarification or add additional context in comments.

4 Comments

setFile replaces existing scripts with given one
Oh, ok. Thanks, will check it tomorrow, after holiday. According to the sources you're right, thank you
However, if you want to stick with captureStart() captureEnd() construction, you might be interested in this solution: stackoverflow.com/questions/10311281/… hope this helps
yep, I thought about writing a custom helper but it doesn't worth it
1

The issue is separation of multiple .js sections. This is totally doable as the viewhelpers for headlink, headscript, etc. implement the ArrayAccess interface.

This is how I do it - using the ZF2 Bootstrap (from Skeleton to be consistent):

<!-- 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()); ?> 

This should help tremendously.

Comments

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.

4 Comments

Yep, but what I need is to split the output into 2 pieces :-)
use an inline script or the head scripts will post in the order you include them.
Maybe if you present your exact problem I can be of more help.
added explanation of why I need that :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.