AbanteCart Community

AbanteCart Development => Extensions and Add-Ons => General Extensions => Topic started by: Nimitz1061 on July 22, 2012, 12:26:27 PM

Title: Placing Asynchronous scripts globally
Post by: Nimitz1061 on July 22, 2012, 12:26:27 PM
I've been looking at the developers documentation.

We need to place an asynchronous script (two separate script tags, one initializing things for the other, which provides the final service).    The method provided for placing scripts seems to only support adding scripts into the header.

Am I missing something here??

David
Title: Re: Placing Asynchronous scripts globally
Post by: abolabo on July 23, 2012, 04:06:57 AM
hello.
1. You can place scripts from controller.
2. You can pace "script" tags in your tpl-file.
1st way is prefer.

If you want to place scripts globally you can add it into common/head (php or tpl) file.

i don't know what you doing but if you working on template you can hook controller common/head.php.
for ex. your extension calls mytemplate:
1. create file with hook in directory mytemplate/core/hook.php and include it into main.php
Code: [Select]
(if(!class_exists('ExtensionMyTemplate')){
include('core/hook.php');
})
2. place there hook something like this
Code: [Select]
class ExtensionMyTemplate extends Extension {
   
     public function onControllerCommonHead_UpdateData() {
$registry = Registry::getInstance();
$registry->get('document')->addScript($this->view->templateResource('/javascript/global_scripts/myscript.js'))
    }
}
Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on July 23, 2012, 06:49:28 AM
This places a single script into the head of the page.

An asynchronous script is split into two (or more) script blocks at least one of which is placed in the foot of the page. (ie, just before the closing of the body).

Also, there are many instances in which placing a script at the bottom of the page is desirable.  I don't see a method which will accomplish this.

David
Title: Re: Placing Asynchronous scripts globally
Post by: abolabo on July 23, 2012, 07:15:26 AM
This places a single script into the head of the page.

An asynchronous script is split into two (or more) script blocks at least one of which is placed in the foot of the page. (ie, just before the closing of the body).

Also, there are many instances in which placing a script at the bottom of the page is desirable.  I don't see a method which will accomplish this.

David

sorry, but i don't understand what's problem.
First script adds to head.
Second  script tag  - into common/page.tpl before body tag close.

 
Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on July 23, 2012, 09:32:06 AM
I should add that global means global.  Happening on every page, in every template no matter what.

Analytics, for example,  is not a template related activity, and we need to not lose the script because someone choses a different template or layout.  We also need to assure that the blocks placed for global features are not deleted or removed from display.

Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on July 23, 2012, 11:44:41 AM
Looks like we need some sort of hook.  Question being which is the best one..

Looked at:

Code: [Select]
http://www.abantecart.com/document_wiki/index.php/AbanteCart_Extension%E2%80%99s_Developer_Guide#Hooks
Which is still not terribly clear about hook names..

In :

Code: [Select]
class ControllerCommonPage extends AController {

public function main() {

//init controller data
$this->extensions->hk_InitData($this,__FUNCTION__);

.
.
.

        //init controller data
        $this->extensions->hk_UpdateData($this,__FUNCTION__);
}
}

Would the required  hook names be  onControllerCommonPageMain_InitData and onControllerCommonPageMain_UpdateData  or ??

David


Title: Re: Placing Asynchronous scripts globally
Post by: abantecart on July 24, 2012, 08:56:58 AM
if you want to hook to ControllerCommonPage you do it with hk name onControllerCommonPage_InitData, but I suggest alternative approach.

Since you need to add to beginning and end on the body tag, you need to use addScript as suggested earlier to add to head, and add variable hook to common/page.tpl
Check Hooks Template Variables section of the same manual. We still working to improve manuals or waiting for help on that :) , so excuse for lack of clarity.

Since there is no variable in core tpl, we can include it in the core distribution, since I think it might be useful. Can you please indicate where exactly you need it?

Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on July 24, 2012, 12:14:24 PM
This would need to be just before the closing body tag, on all pages.

David
Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on July 24, 2012, 01:47:50 PM
BTW - there is a strong recommendation by a number of page optimization people to place any script which does not need to be executed on Body load in the foot of the page, rather than in the header.  Might be a good idea to have separate methods for script loading which allow for this.  something like

Code: [Select]
$registry->get('document')->addScriptHead($this->view->templateResource('/javascript/global_scripts/myscript.js'))
and

Code: [Select]
$registry->get('document')->addScriptFoot($this->view->templateResource('/javascript/global_scripts/myscript.js'))
Though, I'm still a bit curious as to how you do this with scripts generated on the fly, as for various analytics platforms.....

David
Title: Re: Placing Asynchronous scripts globally
Post by: abolabo on July 24, 2012, 03:33:21 PM
Though, I'm still a bit curious as to how you do this with scripts generated on the fly, as for various analytics platforms.....

Try to change page.tpl and add <?php echo $your_var_name; ?> before </body>
Also create hook on ControllerCommonPage and fill this var like this:
Code: [Select]
$this->baseObject->view->assign('your_var_name','<script type="text/javascript" src="'.$this->baseObject->templateResource('/javascript/global_scripts/myscript.js'.'"></script>';
Will it work for you?

Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on July 25, 2012, 08:01:35 AM
This is close.

We need to assign values to select Javascript variables before sending the script to the client.

Something like:

Code: [Select]

 $this->view->assign('our_variable', $script_data);   


Would be more appropriate. 
Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on September 22, 2012, 01:18:14 PM
Though, I'm still a bit curious as to how you do this with scripts generated on the fly, as for various analytics platforms.....

Try to change page.tpl and add <?php echo $your_var_name; ?> before </body>
Also create hook on ControllerCommonPage and fill this var like this:
Code: [Select]
$this->baseObject->view->assign('your_var_name','<script type="text/javascript" src="'.$this->baseObject->templateResource('/javascript/global_scripts/myscript.js'.'"></script>';
Will it work for you?


I'm trying to apply the above suggested technique to CommonHead as follows:

Code: [Select]
  public function onControllerCommonHead_UpdateData() {
// $registry = Registry::getInstance();
//$registry->get('document')->addScript($this->view->templateResource('/javascript/global_scripts/myscript.js'))
   $this->baseObject->view->assign('your_var_name','<strong>Splat</strong>');
    }
 }

with this code located within a class in my extensions core/x.php file.

Its not working.

I do have the echo for your_var_name in the common/head.php view.

Any suggestions ??

David
Title: Re: Placing Asynchronous scripts globally
Post by: abolabo on September 24, 2012, 06:11:24 AM

I'm trying to apply the above suggested technique to CommonHead as follows:

Code: [Select]
  public function onControllerCommonHead_UpdateData() {
// $registry = Registry::getInstance();
//$registry->get('document')->addScript($this->view->templateResource('/javascript/global_scripts/myscript.js'))
   $this->baseObject->view->assign('your_var_name','<strong>Splat</strong>');
    }
 }

with this code located within a class in my extensions core/x.php file.

Its not working.

I do have the echo for your_var_name in the common/head.php view.

Any suggestions ??

David

may be you mean var_dump(your_var_name) inside common/head.TPL ?
Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on September 24, 2012, 07:11:04 AM
No, I mean echo.  As in:

Code: [Select]
<?php echo $this->getHookVar('your_var_name'); ?>
<?php echo $your_var_name?>

with no resulting output.

And no errors related to the module found in the error.log
Title: Re: Placing Asynchronous scripts globally
Post by: abolabo on September 24, 2012, 09:27:28 AM
No, I mean echo.  As in:

Code: [Select]
<?php echo $this->getHookVar('your_var_name'); ?>
<?php echo $your_var_name?>

with no resulting output.

And no errors related to the module found in the error.log
yes, i c.
Where did you placed this code? in controller or in tpl-file?
Title: Re: Placing Asynchronous scripts globally
Post by: Nimitz1061 on September 24, 2012, 11:31:09 AM
In the head.tpl file.