Please help us to make AbanteCart Ideal Open Source Ecommerce Solution for everyone.

Support AbanteCart eCommerce

Author Topic: Placing Asynchronous scripts globally  (Read 24762 times)

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Placing Asynchronous scripts globally
« 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

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2046
  • Karma: +318/-13
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Placing Asynchronous scripts globally
« Reply #1 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'))
    }
}
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #2 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

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2046
  • Karma: +318/-13
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Placing Asynchronous scripts globally
« Reply #3 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.

 
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #4 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.

« Last Edit: July 23, 2012, 11:17:42 AM by Nimitz1061 »

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #5 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



Offline abantecart

  • Administrator
  • Hero Member
  • *****
  • Posts: 4358
  • Karma: +298/-10
    • View Profile
    • Ideal Open Source Ecommerce Solution
Re: Placing Asynchronous scripts globally
« Reply #6 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?

Please  rate your experience or leave your review
We need your help to build better free open source ecommerce platform for everyone. See how you can help

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #7 on: July 24, 2012, 12:14:24 PM »
This would need to be just before the closing body tag, on all pages.

David

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #8 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

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2046
  • Karma: +318/-13
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Placing Asynchronous scripts globally
« Reply #9 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?

« Last Edit: July 24, 2012, 07:58:08 PM by abantecart »
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #10 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. 
« Last Edit: September 22, 2012, 01:14:38 PM by Nimitz1061 »

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #11 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

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2046
  • Karma: +318/-13
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Placing Asynchronous scripts globally
« Reply #12 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 ?
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

Offline Nimitz1061

  • Full Member
  • ***
  • Posts: 190
  • Karma: +22/-0
  • No matter where you go, there you are...
    • View Profile
Re: Placing Asynchronous scripts globally
« Reply #13 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
« Last Edit: September 24, 2012, 08:05:55 AM by Nimitz1061 »

Offline abolabo

  • core-developer
  • Administrator
  • Hero Member
  • *****
  • Posts: 2046
  • Karma: +318/-13
  • web for all, all for web!
    • View Profile
    • AbanteCart
Re: Placing Asynchronous scripts globally
« Reply #14 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?
“No one is useless in this world who lightens the burdens of another.”
― Charles Dickens

 

Powered by SMFPacks Social Login Mod