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');
Both CSS and JS <style> and <script> tags are generated correctly.
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>
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.
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.
Labels |
Added:
?
|
Labels |
Added:
J3 Issue
|
Status | New | ⇒ | Information Required |
I can't replicate the issue.
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');
@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
.
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
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
Status | Information Required | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2020-02-03 19:15:49 |
Closed_By | ⇒ | alikon |
can another one of Bug Squad Team have a look please?