Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append Javascript File to the end of the InlineScript Collection from child view

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 ?

like image 923
MadeOfSport Avatar asked Feb 08 '13 12:02

MadeOfSport


2 Answers

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.

like image 95
Bertrand Avatar answered Oct 04 '22 23:10

Bertrand


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.

like image 34
ba0708 Avatar answered Oct 04 '22 22:10

ba0708