Page 1 of 1

addStyle and addScript

Posted: Thu May 20, 2010 6:50 pm
by yveveke
In a module Controller file I want to add a css file and javascript document.

I do this trough

Code: Select all

$this->document->addStyle($style);
$this->document->addScript(HTTPS_SERVER . 'catalog/view/javascript/module-tombola.js');
 
Strangely enough the styles and scripts array gets emptied when the header controller file reads it.

Code: Select all

$this->data['styles'] = $this->document->styles;
$this->data['scripts'] = $this->document->scripts;
 
And in extend to the above question, why don't we use the documents' addBreadcrumb function instead of

Code: Select all

$this->document->breadcrumbs = array();

$this->document->breadcrumbs[] = array(
'href'      => HTTPS_SERVER . 'index.php?route=common/home',
'text'      => $this->language->get('text_home'),
'separator' => FALSE
);

$this->document->breadcrumbs[] = array(
'href'      => HTTPS_SERVER . 'index.php?route=extension/module',
'text'      => $this->language->get('text_module'),
'separator' => ' :: '
);
        
$this->document->breadcrumbs[] = array(
'href'      => HTTPS_SERVER . 'index.php?route=module/bestseller',
'text'      => $this->language->get('heading_title'),
'separator' => ' :: '
);
 
Are those functions getting deprecated?

I'll add the css and js file in the header.tpl for now, but would like to do it clean.

Re: addStyle and addScript

Posted: Fri May 21, 2010 6:03 pm
by yveveke
Any response?

Are the functions getting deprecated? If not, why aren't they used?
Why is the styles and scripts array empty when the header controller file reads it for output.

Re: addStyle and addScript

Posted: Fri May 21, 2010 8:26 pm
by JNeuhoff
I think the addScript and addStyle functions are only work for top-level controllers, not for lower-level ones like the modules.

Perhaps Qphoria or Daniel can enlighten us on this?

Re: addStyle and addScript

Posted: Fri May 21, 2010 9:18 pm
by Qphoria
Hi, sorry I forgot about this one.
This is actually a children listing order issue. All controllers have this lovely (insert sarcasm here) children array:

Code: Select all

$this->children = array(
    'common/header',
    'common/footer',
    'common/column_left',
    'common/column_right'
); 
The header loads the document info to the page, but the modules are loaded in the column_left and right. Since they are listed AFTER the header, it is just in the wrong order.

Change the order:

Code: Select all

$this->children = array(
    'common/footer',
    'common/column_left',
    'common/column_right',
    'common/header',
); 
and it works.

I will fix this in 1.4.8

Re: addStyle and addScript

Posted: Fri May 21, 2010 11:05 pm
by yveveke
Great, thanks for the response.

Will it be common practice to use your breadcrumb function?

Code: Select all

$this->document->breadcrumbs = array();

$this->document->breadcrumbs[] = array(
'href'      => HTTPS_SERVER . 'index.php?route=common/home',
'text'      => $this->language->get('text_home'),
'separator' => FALSE
);

$this->document->breadcrumbs[] = array(
'href'      => HTTPS_SERVER . 'index.php?route=extension/module',
'text'      => $this->language->get('text_module'),
'separator' => ' :: '
);
        
$this->document->breadcrumbs[] = array(
'href'      => HTTPS_SERVER . 'index.php?route=module/bestseller',
'text'      => $this->language->get('heading_title'),
'separator' => ' :: '
);
 
becomes

Code: Select all

$this->document->addBreadcrumb($this->language->get('text_home'), HTTPS_SERVER . 'index.php?route=common/home', FALSE);
$this->document->addBreadcrumb($this->language->get('text_module'), HTTPS_SERVER . 'index.php?route=extension/module', ' :: ');
$this->document->addBreadcrumb($this->language->get('heading_title'), HTTPS_SERVER . 'index.php?route=module/bestseller', ' :: ');
 

Re: addStyle and addScript

Posted: Fri May 21, 2010 11:20 pm
by Qphoria
Yes, I think the reason was that add function was added after the fact when the initial layout was made. So we just haven't gotten around to updating all places to use it.

Re: addStyle and addScript

Posted: Fri Jul 23, 2010 3:09 pm
by benjamin
Hi,

the new option to place modules in the home area, this get's broken again.
Using $this->document->addScripts() in a module placed in the home area has no effect to the header. The module controller is executed when the header is already prepared.

What about something like a "init()" method for the controllers? This could be called on all controllers on the current page before any output generation takes place?

Cheers
Benjamin

Re: addStyle and addScript

Posted: Mon Aug 09, 2010 11:53 pm
by Qphoria
benjamin wrote:Hi,

the new option to place modules in the home area, this get's broken again.
Using $this->document->addScripts() in a module placed in the home area has no effect to the header. The module controller is executed when the header is already prepared.

What about something like a "init()" method for the controllers? This could be called on all controllers on the current page before any output generation takes place?

Cheers
Benjamin
This was fixed in 1.4.8 btw

Re: addStyle and addScript

Posted: Fri Nov 12, 2010 2:33 pm
by rsepulvedacl
Hi guys!
I just made some changes to the file ./catalog/controller/common/home.php

Original:

Code: Select all

        $this->children = array(
            'common/column_right',
            'common/column_left',
            'common/footer',
            'common/header'
        );
        
        $this->load->model('checkout/extension');
        
        $module_data = $this->model_checkout_extension->getExtensionsByPosition('module', 'home');
        
        $this->data['modules'] = $module_data;

        foreach ($module_data as $result) {
            $this->children[] = 'module/' . $result['code'];
        } 
Modified:

Code: Select all

        $this->load->model('checkout/extension');
        
        $module_data = $this->model_checkout_extension->getExtensionsByPosition('module', 'home');
        
        $this->data['modules'] = $module_data;

        foreach ($module_data as $result) {
            $this->children[] = 'module/' . $result['code'];
        }

        $this->children[] = 'common/column_right';
        $this->children[] = 'common/column_left';
        $this->children[] = 'common/footer';
        $this->children[] = 'common/header'; 
And my module loaded into home worked with it's given scripts. :crazy:

Re: addStyle and addScript

Posted: Sun Nov 14, 2010 1:02 am
by Qphoria
Well 2 things.. The new code and the old code are the same.. just moved around, but functionally the same.
Except, you never defined $this->children as an array, so you likely have some php warnings in your error log.

I don't see what the change is for tho. There was nothing wrong with the code

Re: addStyle and addScript

Posted: Sun Nov 14, 2010 1:27 am
by rsepulvedacl
The change was because I was able to load a JavaScript code from a module into the header with this hook:

Code: Select all

$this->document->addScript('catalog/view/javascript/jquery/cycle/jquery.cycle.min.js'); 
But it only worked when the module was into the left or right column, not into the home. This was because the header is called before the module from the file ./catalog/controller/common/home.php.

Some time ago you published a PDF file with the hooks, and that file reads 'Called from controller only before renderer', and as render(TRUE) function renders children in order, this one was rendering header before my module and then the JavaScript code wasn't loaded as expected (when you setup that module into home, not left or right columns).

I made a dirty change into your code, and it worked. The JavaScript was also loaded when I setup my module into home too.

If you want an example, please tell me. :)

Re: addStyle and addScript

Posted: Sun Nov 14, 2010 2:05 am
by Qphoria
Ah I see now. Ok your change does make sense. In 150 it will likely change back to the old layout so this should no longer be an issue in future versions

Re: addStyle and addScript

Posted: Sun Nov 14, 2010 2:38 am
by rsepulvedacl
:)

Re: addStyle and addScript

Posted: Sun Nov 14, 2010 2:52 am
by JNeuhoff
In 150 it will likely change back to the old layout so this should no longer be an issue in future versions
What do mean by old layout ?

Re: addStyle and addScript

Posted: Sun Nov 14, 2010 6:44 am
by Qphoria
1.3.2 and earlier: layout.php/layout.tpl.
No more sections as children of the controller BS

Re: addStyle and addScript

Posted: Mon Nov 15, 2010 1:53 am
by JNeuhoff
Qphoria wrote:1.3.2 and earlier: layout.php/layout.tpl.
No more sections as children of the controller BS
Very good!

Re: addStyle and addScript

Posted: Tue Jun 02, 2020 9:19 am
by nightwing
Hi - I know this is an old post (I am not a coding expert as yet) but can this be deferred or asynced?
I have the below in one of my controller files for a custom module and I am trying to optimize the site, similar to what you have:

Code: Select all

$this->document->addScript('catalog/view/javascript/file.min.js');
Thanks
rsepulvedacl wrote:
Sun Nov 14, 2010 1:27 am
The change was because I was able to load a JavaScript code from a module into the header with this hook:

Code: Select all

$this->document->addScript('catalog/view/javascript/jquery/cycle/jquery.cycle.min.js'); 
But it only worked when the module was into the left or right column, not into the home. This was because the header is called before the module from the file ./catalog/controller/common/home.php.

Some time ago you published a PDF file with the hooks, and that file reads 'Called from controller only before renderer', and as render(TRUE) function renders children in order, this one was rendering header before my module and then the JavaScript code wasn't loaded as expected (when you setup that module into home, not left or right columns).

I made a dirty change into your code, and it worked. The JavaScript was also loaded when I setup my module into home too.

If you want an example, please tell me. :)

Re: addStyle and addScript

Posted: Wed Jul 01, 2020 3:04 am
by nightwing
Hi There,
Does anyone know how to defer or async scripts and styles from the controller file?
nightwing wrote:
Tue Jun 02, 2020 9:19 am
Hi - I know this is an old post (I am not a coding expert as yet) but can this be deferred or asynced?
I have the below in one of my controller files for a custom module and I am trying to optimize the site, similar to what you have:

Code: Select all

$this->document->addScript('catalog/view/javascript/file.min.js');
Thanks
rsepulvedacl wrote:
Sun Nov 14, 2010 1:27 am
The change was because I was able to load a JavaScript code from a module into the header with this hook:

Code: Select all

$this->document->addScript('catalog/view/javascript/jquery/cycle/jquery.cycle.min.js'); 
But it only worked when the module was into the left or right column, not into the home. This was because the header is called before the module from the file ./catalog/controller/common/home.php.

Some time ago you published a PDF file with the hooks, and that file reads 'Called from controller only before renderer', and as render(TRUE) function renders children in order, this one was rendering header before my module and then the JavaScript code wasn't loaded as expected (when you setup that module into home, not left or right columns).

I made a dirty change into your code, and it worked. The JavaScript was also loaded when I setup my module into home too.

If you want an example, please tell me. :)