Placing Asynchronous scripts globally

Started by Nimitz1061, July 22, 2012, 12:26:27 PM

Previous topic - Next topic

Nimitz1061

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

abolabo

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 (if(!class_exists('ExtensionMyTemplate')){
include('core/hook.php');
})

2. place there hook something like this
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

Nimitz1061

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

abolabo

Quote from: 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

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

Nimitz1061

#4
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.


Nimitz1061

Looks like we need some sort of hook.  Question being which is the best one..

Looked at:

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 :

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



abantecart

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

Nimitz1061

This would need to be just before the closing body tag, on all pages.

David

Nimitz1061

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

$registry->get('document')->addScriptHead($this->view->templateResource('/javascript/global_scripts/myscript.js'))

and

$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

abolabo

#9
Quote from: Nimitz1061 on July 24, 2012, 01:47:50 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:
$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?

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

Nimitz1061

#10
This is close.

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

Something like:



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



Would be more appropriate. 

Nimitz1061

Quote from: abolabo on July 24, 2012, 03:33:21 PM
Quote from: Nimitz1061 on July 24, 2012, 01:47:50 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:
$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:

  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

abolabo

Quote from: Nimitz1061 on September 22, 2012, 01:18:14 PM

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

  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

Nimitz1061

#13
No, I mean echo.  As in:

<?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

abolabo

Quote from: Nimitz1061 on September 24, 2012, 07:11:04 AM
No, I mean echo.  As in:

<?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

Forum Rules Code of conduct
AbanteCart.com 2010 -