J3 Issue ?
avatar grantg182
grantg182
27 Jul 2019

Steps to reproduce the issue

In a mod_banners template override, I want to load both a CSS and JS file for this template only. Therefore I use addStyleSheet and addScript like so:-

$document = JFactory::getDocument();
define("PATH", "/templates/mytemplates/");
$document->addStyleSheet(PATH . '/css/mystyles.css');
$document->addScript(PATH . '/js/myscript.js');

Expected result

Both CSS and JS <style> and <script> tags are generated correctly.

Actual result

addStyleSheet works fine. addScript returns the path prepended with DOCUMENT_ROOT, ie:-

<link href="/templates/mytemplate/css/mystyles.css" rel="stylesheet" type="text/css">

<script src="/file/system/path/to/htdocs/templates/mytemplate/js/myscript.js" type="text/javascript"></script>

System information (as much as possible)

Tested on Joomla 3.6.5 and 3.9.8 on the same server. No other instances of addScript or addStyleSheet functions; not modified these functions nor overridden anywhere.

Additional comments

Looking at both addStyleSheet and addScript functions, I cannot fathom for the life of me why this is happening. I've even tried setting the full path in addScript and it still generates as:-
/file/system/path/to/htdocs/templates/mytemplate/js/myscript.js. Same behaviour with and without SEF enabled. Same behaviour setting the RewriteBase in .htaccess.

avatar grantg182 grantg182 - open - 27 Jul 2019
avatar joomla-cms-bot joomla-cms-bot - change - 27 Jul 2019
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 27 Jul 2019
avatar franz-wohlkoenig franz-wohlkoenig - change - 28 Jul 2019
Labels Added: J3 Issue
avatar franz-wohlkoenig franz-wohlkoenig - labeled - 28 Jul 2019
avatar franz-wohlkoenig
franz-wohlkoenig - comment - 28 Jul 2019

can another one of Bug Squad Team have a look please?

avatar franz-wohlkoenig franz-wohlkoenig - change - 28 Jul 2019
Status New Information Required
avatar SharkyKZ
SharkyKZ - comment - 28 Jul 2019

I can't replicate the issue.

avatar richard67
richard67 - comment - 28 Jul 2019

Beside that, the example code in the description leads to a double slash in the final path, because "PATH" variable ends with a slash and is then concatenated with something whish starts also with a slash:

define("PATH", "/templates/mytemplates/");
$document->addStyleSheet(PATH . '/css/mystyles.css');
$document->addScript(PATH . '/js/myscript.js');

avatar grantg182
grantg182 - comment - 28 Jul 2019

@richard67 correct; my bad; poor copy/paste & inline editing on my part.

For what it's worth, I have temporarily solved this by modifying my $PATH constant. I was initially using a constant which is defined upstream. I have changed this to use another constant to partially resolve the issue.
OLD:-
define('PATH', str_replace ('\\', '/', JPATH_ROOT) . '/templates/mytemplate');
NEW:-
define('PATH', JURI::root(true).'/templates/mytemplate');

There is still a bug in existence somewhere, because using the OLD variant produced different results as described in the OP between addStyleSheet and addScript.

avatar ghazal
ghazal - comment - 29 Jul 2019

Try this other (and endorsed) way to add js and css files in joomla.
In your mod_banners template override file (default.php), insert:

defined('_JEXEC') or die;
JHtml::_('stylesheet', 'myassets/yourcss.css', array('version' => 'auto', 'relative' => true));

JHtml::_('script', 'myassets/yourjs.js', array('version' => 'auto', 'relative' => true));

While in /media/, you created a /myassets/ folder and inside this folder, a /css/ and a /js/ folders where you put your files.

NB Use
JHtml::_('jquery.framework');
in case your script in jQuery-dependant et to be sure it is loaded after jQuery

avatar alikon
alikon - comment - 3 Feb 2020

Closing this due to not receiving required information to determine if this is a bug or not. If you feel this still needs review, please open a new tracker entry with as much information as possible to ensure it can be reviewed properly

avatar alikon alikon - change - 3 Feb 2020
Status Information Required Closed
Closed_Date 0000-00-00 00:00:00 2020-02-03 19:15:49
Closed_By alikon
avatar alikon alikon - close - 3 Feb 2020

Add a Comment

Login with GitHub to post a comment