Page 1 of 1
[CLOSED] Loading a module to header.php with addstyle and addScript
Posted: Fri May 05, 2017 7:10 pm
by chaptan
Hi, I've added a module to my header.php. But for some reason the module document->addstyle and addscript not working. For the moment, i have had to add the scripts via a modification .Any ideas why it doesnt work via the module?
Re: Loading a module to header.php with addstyle and addScript
Posted: Mon May 08, 2017 9:35 am
by IP_CAM
this may help, to get it right!
Good Luck
Ernie
---
https://www.opencart.com/index.php?rout ... n_id=19455
Code: Select all
<?php
class ControllerModuleLbNotify extends Controller {
public function index($setting) {
$this->language->load('module/lbnotify');
// Stylesheets
if (file_exists('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/lbnotify/lbnotify.css')) {
$this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/lbnotify/lbnotify.css');
} else {
$this->document->addStyle('catalog/view/theme/default/stylesheet/lbnotify/lbnotify.css');
}
//JavaScript
$this->document->addScript('catalog/view/javascript/lbnotify/lbnotify.js');
$description = isset( $setting['description'][$this->config->get('config_language_id')] ) ? $setting['description'][$this->config->get('config_language_id')] : "";
Re: Loading a module to header.php with addstyle and addScript
Posted: Mon May 08, 2017 5:05 pm
by chaptan
IP_CAM
This is my code v2.3.0.2:
Code: Select all
<?php
class ControllerExtensionModuleIconMenu extends Controller {
private $_route = 'extension/module/icon_menu';
public function index() {
$data['template'] = $this->config->get('config_theme');
//ADD-ON SCRIPTS(doesn't work for some reason added in header.tpl instead)
$this->document->addStyle('catalog/view/theme/'.$data['template'].'/js/'.$this->_route.'/fisheye.css');
$this->document->addScript('catalog/view/theme/'.$data['template'].'/js/'.$this->_route.'/fisheye.js');
The paths are correct.
Its not really a problem because it is part of the theme anyway. But i just wondered why when adding a module into the catalog\controller\common\header.php via a modification:
Code: Select all
$data['icon_menu'] = $this->load->controller('extension/module/icon_menu');
addStyle and addscript doesn't work.
Its probably a loading thing ,addStyle and addscript doesn't work, when a module is added this way?
Not a problem, it works when i add the scripts to the theme header.tpl.Just wondered why?
Thanks for your reply.
Re: Loading a module to header.php with addstyle and addScript
Posted: Tue May 09, 2017 7:40 am
by IP_CAM
sorry, I am not a Coder, and v.2.3.x is an unknown 'Member' to me anyway,
But just to make sure, did you CLEAR OUT your OcModification and VqCache
section,
every time, after you made changes ? Because otherways, the
system will still 'present' the old cached File-Content.
Good Luck
Ernie
Re: Loading a module to header.php with addstyle and addScript
Posted: Sun Nov 24, 2024 10:38 am
by madmaxb
Add your module initialization before this section of header controller
So header controller should finally look like this:
Code: Select all
.....
$data['icon_menu'] = $this->load->controller('extension/module/icon_menu');
$data['links'] = $this->document->getLinks();
$data['styles'] = $this->document->getStyles();
$data['scripts'] = $this->document->getScripts('header');
$data['lang'] = $this->language->get('code');
$data['direction'] = $this->language->get('direction');
......
When you write your modification XML just do this:
Code: Select all
<file error="log" path="catalog/controller/common/header.php">
<operation error="log">
<search index="0">
<![CDATA[$data['links'] = $this->document->getLinks();]]>
</search>
<add position="before">
<![CDATA[
$data['icon_menu'] = $this->load->controller('extension/module/icon_menu');
]]>
</add>
</operation>
</file>
Everything will start working.
Re: Loading a module to header.php with addstyle and addScript
Posted: Sun Nov 24, 2024 7:03 pm
by JNeuhoff
madmaxb wrote: ↑Sun Nov 24, 2024 10:38 am
Everything will start working.
Well, rest assured that the original poster from seven years ago has long since then found a suitable solution
