Add Javascript to end of InlineScript collection from child view

I am working with Zend Framework 2.

In my layout file, I insert some javascript files, for example:

$this->InlineScript() ->appendFile($this->basePath() . '/js/myfile.js'); echo $this->InlineScript(); 

Now I want to insert some javascript from the view so that it is added to the end of the InlineScript collection.

So, I wrote this in my action:

 <?php $this->InlineScript()->offsetSetFile(100,$this->basePath() . '/js/xyz.js'); ?> 

But the result is loading the xyz file first in the rendered view.

I am working with Zend Framework 2.0.5

Can anyone give me some advice on how to do this?

+4
source share
4 answers

Just to complement this old question:

Interior view: At the top of the page, add the file :

 $this->headScript()->appendFile('/js/filename.js'); 

Add script at the top of the page

 $this->headScript()->appendScript('alert(1)'); 

Add file at the bottom of the page:

 $this->inlineScript()->appendFile('/js/filename.js'); 

Add script at the bottom of the page

 $this->inlineScript()->appendScript('alert(1)'); 

Internal controller / action

Take Headscript with serviceLocator and the rest will be 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.

+13
source

This is apparently caused by using appendFile in your layout. First, your script view opens, in which you add the script to the stack. Then, when your layout is launched, you add it again, making the script from your layout last. Try using prependScript in your layout file so that the script from your layout does not add to scripts already added.

+2
source

I know this is an old question, but this explanation can be really useful (I ruined my head this problem several times ..)

Let's start with the case:

You have 4 scripts to add to the page.

2 are shown in your layout and 2 in your view.

Name them script1, script2, script3 and script4 (the final order should respect the numbering).

Layout:

 <?php $this->inlineScript()->prependFile('script2.js') ->prependFile('script1.js'); 

View:

 <?php $this->inlineScript()->appendFile('script3.js') ->appendFile('script4.js'); 

Result:

 <script type="text/javascript" src="/script1.js"></script> <script type="text/javascript" src="/script2.js"></script> <script type="text/javascript" src="/script3.js"></script> <script type="text/javascript" src="/script4.js"></script> 

What happened

First, the view is processed, scripts (in the view) are added to the stack, so we have:

script3 added:

 [script3.js] <-- 

script4 added:

 [script2.js] [script3.js] <-- 

Then the layout is processed and the script is added in reverse order, therefore:

script2 added:

 [script2.js] <-- [script3.js] [script4.js] 

script1 added:

 [script1.js] <-- [script2.js] [script3.js] [script4.js] 
+1
source

I worked on a search: " If you know how to get inlineScrip inside an action, please let us know. ", Got a pretty good solution and decided to post it here:

  • Create a small class that will be called elsewhere to group all your inline scripts and format them correctly for display using the layout.phtml template
  • Define a conditional insertion point to insert full scripts on the last page

(I am replacing the code 04-05-2016 because the original was sealed)

  <? php
         // Filename: /module/MyTools/src/MyTools/Library/Js2ls.php
         namespace MyTools \ Library;    
         abstract class Js2l
 {
     protected static $ inLineJs;
     protected static $ inLineVars;

     public static function addSetting ($ settingkey, $ settingvalue)
     {
         if (! self :: isSettingThere ($ settingkey)) {
             self :: $ inLineVars [$ settingkey] = $ settingvalue;
         }
     }

     public static function addScript ($ script, $ scriptkey = NULL)
     {
         if (! $ scriptkey) {
             if (! self :: $ inLineJs) {
                 self :: $ inLineJs = array ();
                 self :: $ inLineJs [] = $ script;
             } elseif (! (in_array ($ script, self :: $ inLineJs))) {
                 self :: $ inLineJs [] = $ script;
             }
         } elseif (! isset (self :: $ inLineJs [$ scriptkey])) {
             self :: $ inLineJs [$ scriptkey] = $ script;
         }
     }

     protected static function appendItemToSetting ($ current, $ item)
     {
         if ($ item == $ current) {return $ current;  }
         if (is_array ($ current)) {
             if (! in_array ($ item, $ current)) {                
                 $ current [] = $ item;
             }
             return $ current;
         } else {
             return [$ current, $ item];
         }
     }

     public static function extendSetting ($ settingkey, $ settingvalue)
     {
         $ current = self :: getSetting ($ settingkey);
         if ($ current) {
             $ new = self :: appendItemToSetting ($ current, $ settingvalue);
             if ($ new! == $ current) {
                 self :: $ inLineVars [$ settingkey] = $ new;            
             }
         } else {
             self :: addSetting ($ settingkey, $ settingvalue);
         }
     }
     public static function getSetting ($ settingkey) {
         if (self :: $ inLineVars) {
           $ sale = isset (self :: $ inLineVars [$ settingkey])?  self :: $ inLineVars [$ settingkey]: NULL;
           return $ sale; 
         } else {
           return NULL;
         }
     }

     public static function getScripts () {
       return self :: $ inLineJs;
     }

     public static function getSettings () {
       return self :: $ inLineVars;
     }

     public static function iskeyedArray ($ array)
     {
         $ out = FALSE;
         $ keys = array_keys ($ array);
         foreach ($ keys as $ key) {
             if (! is_numeric ($ key)) {
                 $ out = TRUE;
                 break;
             }
         }
         return $ out;
     }

     protected static function renderInLineJs ()
     {
         return (self :: $ inLineJs)?  implode ("\ n", self :: $ inLineJs): '';
     }

     protected static function renderInLineVars ($ js)
     {
         $ out = '';
         if (is_array ($ js)) {
             $ items = [];
             foreach ($ js as $ key => $ value) {
                 $ items [] = '"'. $ key. '":' .self :: renderJsItem ($ value);
             }
             $ out. = implode (',', $ items);
         }        
         return $ out;
     }

     protected static function renderJsItem ($ item)
     {
         if (is_scalar ($ item)) {
             return (is_numeric ($ item))?  ''. $ item: '"'. $ item. '"';
         } elseif (is_array ($ item)) {
             if (self :: iskeyedArray ($ item)) {
                 return '{'.  self :: renderInLineVars ($ item). '}';
             } else {
                 return (count ($ item)> 0)?  '["' .implode ('", "', $ item). '"]': '[]';
             }
         } else {
             return '[]';
         }
     }

     protected static function isSettingThere ($ settingkey)
     {
         return (isset (self :: $ inLineVars [$ settingkey]))?  TRUE: FALSE
     }

     public static function ToString ()
     {
         $ prefix = "\ n". 'jQuery.extend (MyTools.settings, {';
         $ suffix = '});'. "\ n";
         $ settings = self :: renderInLineVars (self :: $ inLineVars);
         $ jsSettings = ($ settings)?  $ prefix. $ settings. $ suffix: '';
         $ jsMethods = self :: renderInLineJs ();
         return ($ jsMethods)?  $ jsSettings. "\ n". $ jsMethods: $ jsSettings;
     }
 }
     ?>

 </code>

 Whit this class defined you can add, elsewhere, global variables to be used by your scripts to a global variable using the code:

 <code>

     <? php
     // you are elsewhere in your php code.  Write
     use MyTools \ Library \ Js2l
     Js2l :: addSetting ($ settingkey, $ settingvalue);
     // or
     Js2l :: extendSetting ($ variablename, $ value);
     // $ settingvalue and $ value can be scalar o array values
     Js2l :: addScript ($ yourJavaScript, $ akeyToAvoidDuplicates);
     ?>

 </code>

 The class knows how to format the code so you can call it to be injected in the template

 <code>
     <? php
     // Filename: /module/Application/view/application/layout/layout.phtml
     ?>

     <? php
     // place this call before the scripts block
     use MyTools \ Library \ Js2l
     $ mysript = Js2l :: ToString ();
     if ($ mysript) {
       echo $ this-> inlineScript () -> appendScript ($ myscript);
     }

 </code>

You now have a useful class for entering inline scripts on demand.

0
source

All Articles