xoops forums

trabis

Core Developer
Posted on: 2010/7/24 17:47
trabis
trabis (Show more)
Core Developer
Posts: 2261
Since: 2006/9/1 1
#1

How to correctly add scripts in your XOOPS theme

If you want to add scripts or stylesheets in your theme do not hardcoded them in the header.

Hardcoding it will probably cause duplication if some other module include the same file.

The solution is using Xoops API that already handles duplicate files.

So instead of just using <{$xoops_module_header}> ,you need to do the following:
<!-- RMVadded module header -->
  <{
php}>
    global 
$xoTheme;
    
$xoTheme->addScript('http://yoursite.com/somescript.js');
    
$xoTheme->addStyleSheet('http://yoursite.com/somesstyle.css');
    
$this->assign('xoops_module_header'$xoTheme->renderMetas(nulltrue));
  <{/
php}>
  <{
$xoops_module_header}>


Have fun!

Catzwolf

Home away from home
Posted on: 2010/7/24 18:02
Catzwolf
Catzwolf (Show more)
Home away from home
Posts: 1392
Since: 2007/9/30
#2

Re: How to correctly add scripts in your XOOPS theme

You don't need the full url there, just

Quote:

$xoTheme->addScript('somescript.js');


$xoTheme will add the url automatically for you. :)

trabis

Core Developer
Posted on: 2010/7/25 13:53
trabis
trabis (Show more)
Core Developer
Posts: 2261
Since: 2006/9/1 1
#3

Re: How to correctly add scripts in your XOOPS theme

There is some code missing for legacy support.
Better solution can be found here:
http://www.xuups.com/modules/mynews/article.php?storyid=17

Catzwolf

Home away from home
Posted on: 2010/7/25 14:50
Catzwolf
Catzwolf (Show more)
Home away from home
Posts: 1392
Since: 2007/9/30
#4

Re: How to correctly add scripts in your XOOPS theme

I fear this will lead to more problem in the long run.

I really do not understand this approach when this should have been handled by the template class? Really, xoTheme should check whether or not the file has been added before and take the required steps to prevent compatibility issue.

Instead we create another file and class to do this and thus instead of making the process simple we begin to complicate the issue further.

Doing this is simple:
$xoTheme->addScript('somescript.js');

Doing this begins to create problems:

$xoTheme->addScript('browse.php?Frameworks/jquery/jquery.js');

Why? Because we could have easily did a check to see whether the javascript was 'JQuery.js' and used the correct version from xoTheme or template class and this way compatibility wouldn't have been affected in the short or long run.

Or better still:

$xoTheme->addJQuery();
$xoTheme->addJQueryUI();

Again we complicate things when there is no need to.