I'm working with Zend Framework 2.
In my Layout File i inject some javascript files like this:
$this->InlineScript()
->appendFile($this->basePath() . '/js/myfile.js');
echo $this->InlineScript();
Now i want to inject some javascript from a view so that it appends to the end of the InlineScript Collection.
So i wrote this in my action view:
<?php $this->InlineScript()->offsetSetFile(100,$this->basePath() . '/js/xyz.js'); ?>
But the result is the File xyz is loaded first in the rendered view.
I'm working with Zend Framework 2.0.5
Does anybody can give me an advise how to manage this ?
Just to complement this old question:
Inside View: Append a file at the top of the page:
$this->headScript()->appendFile('/js/filename.js');
Append a script at the top the page
$this->headScript()->appendScript('alert(1)');
Append a file at the bottom of the page:
$this->inlineScript()->appendFile('/js/filename.js');
Append a script at the bottom of the page
$this->inlineScript()->appendScript('alert(1)');
Inside Controller/Action
Grab Headscript using serviceLocator and the rest is the same
$this->getServiceLocator()
->get('viewhelpermanager')
->get('HeadScript')->appendScript('alert(1)'); //or ->appendFile('/js/filename.js');
If you know how to get inlineScrip inside an action please let us know.
This seems to be caused by using appendFile
within your layout. Your view script is run first in which you append a script to the stack. Then, when your layout is run, you append again, making the script from your layout the last one. Try to use prependScript
in your layout file such that the script from your layout is not appended to the already added scripts.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With