Post by yveveke » Thu May 20, 2010 6:50 pm

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.

Newbie

Posts

Joined
Wed Mar 17, 2010 6:15 pm
Location - Belgium

Post by yveveke » Fri May 21, 2010 6:03 pm

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.

Newbie

Posts

Joined
Wed Mar 17, 2010 6:15 pm
Location - Belgium

Post by JNeuhoff » Fri May 21, 2010 8:26 pm

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?

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Qphoria » Fri May 21, 2010 9:18 pm

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

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by yveveke » Fri May 21, 2010 11:05 pm

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', ' :: ');
 

Newbie

Posts

Joined
Wed Mar 17, 2010 6:15 pm
Location - Belgium

Post by Qphoria » Fri May 21, 2010 11:20 pm

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.

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by benjamin » Fri Jul 23, 2010 3:09 pm

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

--------------------------------------------------------
Bar54 | Toys in Capetown | Catering in Stuttgart


Newbie

Posts

Joined
Wed May 12, 2010 6:12 am
Location - Germany

Post by Qphoria » Mon Aug 09, 2010 11:53 pm

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

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by rsepulvedacl » Fri Nov 12, 2010 2:33 pm

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:

User avatar
New member

Posts

Joined
Tue Jun 29, 2010 8:52 am

Post by Qphoria » Sun Nov 14, 2010 1:02 am

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

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by rsepulvedacl » 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. :)

User avatar
New member

Posts

Joined
Tue Jun 29, 2010 8:52 am

Post by Qphoria » Sun Nov 14, 2010 2:05 am

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

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by rsepulvedacl » Sun Nov 14, 2010 2:38 am

:)

User avatar
New member

Posts

Joined
Tue Jun 29, 2010 8:52 am

Post by JNeuhoff » Sun Nov 14, 2010 2:52 am

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 ?

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by Qphoria » Sun Nov 14, 2010 6:44 am

1.3.2 and earlier: layout.php/layout.tpl.
No more sections as children of the controller BS

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by JNeuhoff » Mon Nov 15, 2010 1:53 am

Qphoria wrote:1.3.2 and earlier: layout.php/layout.tpl.
No more sections as children of the controller BS
Very good!

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by nightwing » 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. :)

Regards,
Nightwing
Access to my Free Extensions: https://www.opencart.com/index.php?rout ... =nightwing


Active Member

Posts

Joined
Tue Nov 05, 2019 11:08 pm


Post by nightwing » Wed Jul 01, 2020 3:04 am

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. :)

Regards,
Nightwing
Access to my Free Extensions: https://www.opencart.com/index.php?rout ... =nightwing


Active Member

Posts

Joined
Tue Nov 05, 2019 11:08 pm

Who is online

Users browsing this forum: No registered users and 11 guests