AbanteCart Community
AbanteCart Development => Extensions and Add-Ons => General Extensions => Topic started 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
-
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'))
}
}
-
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
-
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.
-
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.
-
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
-
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?
-
This would need to be just before the closing body tag, on all pages.
David
-
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
-
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?
-
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.
-
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
-
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, 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
-
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?
-
In the head.tpl file.