How to add JS/CSS files to Joomla modules? - joomla

I am starting out with Joomla and am writing a simple Joomla module. I am using some custom CSS and JS in my module. Now when I distribute this module I need my JS/CSS files to go with the ZIP. I have added my files in my module ZIP file.
This is what i need to know -
How do I refer to these CSS/JS files in my module so that even if I distribute the module as a zip i would not have to send the css/js files separately?
I tried looking at different solutions including
http://www.howtojoomla.net/how-tos/development/how-to-add-cssjavascript-to-your-joomla-extension But I was not able to figure out what the URL for the JS/CSS file should be?
I am using Joomla 1.7 hosted on a cloud hosting site.
Thanks

I'd say that the HowToJoomla Site's article pretty much sums up the process.
Here is the process with a few more steps added - hopefully this will help.
I am assuming you have got as far as packaging your extension and have been able to get your css and javascript files to install on the server. They may be in your module folder, or probably more correctly they should be within your modules sub-folder under the /media/ folder.
If after installing the module you can not locate your css and js files it is because you haven't referenced them correctly within your component's xml installation file. This page contains info about the xml installation / manifest file for 1.6/1.7 add-ons although it is for a component: http://docs.joomla.org/Developing_a_Model-View-Controller_%28MVC%29_Component_for_Joomla!1.6_-_Part_01 they are very similar.
Either way - find your files within Joomla's folder structure and make a note of their relative path from the root of the website - ie the folder containing configuration.php
Now somewhere within your module's php - add the line that gets a reference to the JDocument object.
$document = JFactory::getDocument();
Now add the line that adds your javascript to the html head area:
$document->addScript('url/to/my/script.js');
obviously replace 'url/to/my/script.js' with the actual relative path to your javascript.
Now add the line that adds your css to the html head:
$document->addStyleSheet('url/to/my/stylesheet.css');
again adjust the path - it may for example be media/mod_mymodule/mymodule.css (if your module were called 'mymodule').
Only things to be aware of are that you need to add these lineswithin executable php tags NOT within a html area after exiting php mode.

You could add your js/css files to /media folder.
http://docs.joomla.org/Manifest_files#Media_files
Just add to your manifest file:
<files>
...
</files>
<media folder="media" destination="mod_your_module">
<folder>css</folder>
<folder>js</folder>
</media>
Inside your installable package, you should now have the /media folder.
Then add to view file:
$doc =& JFactory::getDocument();
$doc->addScript("/media/mod_your_module/js/script.js");
This article explains the benefits of this approach:
http://blog.joomlatools.com/2008/09/hidden-feature-joomlas-media-folder.html

You could use:
JHTML::script('modules/mod_your_module/js/script.js');
JHTML::stylesheet('modules/mod_your_module/css/stylesheet.css');
This example does not require JFactory::getDocument()

$document = JFactory::getDocument();
$modulePath = JURI::base() . 'modules/mod_your_module_name/';
//Adding JS Files
$document->addScript($modulePath.'js/myscript.js');
//Adding CSS Files
$document->addStyleSheet($modulePath.'css/style.css');

<script type="text/javascript" src="<?php echo JURI::BASE()?>modules/your_module_name/js/your_js_file"></script>
for Css:
<link href="<?php echo JURI::BASE()?>modules/your_module_name/css/your_css _file" type="text/css" media="all" rel="stylesheet">

Related

How to enque scss on Wordpress

I have this test Wordpress site http://test.fluxmusic.net/ and I have my css and js file working, but I can't find how to make scss files work.
SCSS files must be compiled to css first before enqueuing them. You will need to install a plugin like:
https://wordpress.org/plugins/wp-scss/
Hope this helps

Resolving asset location issues using SASS in PHPStorm

I have a project where the basic asset folder structure looks like this:
/css
/css/sass
/js
/images
When I compile the SASS files, it places them into the css folder above. I do it this way to try and keep my directory structure logical and simple.
I'm using relative paths in my SASS files to link to images:
background: url(../images/foobar.png);
However, since the path is relative from the CSS directory, PHPStorm flags it as an error.
Is there any way to configure PHPStorm to be able to recognise assets from a destination path, and not just directly from the SASS file?
Replace:
background: url(../images/foobar.png);
With:
background: image-url('foobar.png');
To use the compass image-url() function. The inspection errors from PHPStorm will then disappear, and compass will automatically generate the correct path based on the image resource root.

yeoman, SCSS, bower server, styles/main.css not found

I wanted today to try scss support of yeoman.
I followed the procedure :
$ yo angular
include Twitter Bootstrap? Yes
use the SCSS version of Twitter Bootstrap with the Compass CSS Authoring Framework? Yes
$ grunt server
And then the default view load but without style formatting. In the console I can see that it cannot find /styles/main.css file.
I have seen that compass put the file in .tmp/styles/main.css, so I tried to change it in index.html. But the same. Moreover there is no .tmp directory in my project folder.
So I ran "grunt build" and loaded the index.html in dist directory in a MAMP server. Same, no css formatting, moreover no error in the console
You shouldn't be putting the '.tmp' path anywhere in your index.html, it should be left pointing to 'styles/main.css' like so:
<!-- build:css(.tmp) styles/main.css -->
<link rel="stylesheet" href="styles/main.css">
<!-- endbuild -->
You should also have your actual style file at 'app/styles/main.scss', which Yeoman should have created for you. When you run 'grunt server' it uses compass to compile all the scss files into a single css file which it temporarily puts under '.tmp', but as long as your scss files are in place you should not need to worry about this. My guess would be that you've somehow deleted or renamed the main.scss file; you should not be renaming this to 'main.css' for example.
I had the same issue, your main.scss file has issues with bootstrap css
I deleted the line ( 2. line )
#import 'bootstrap-sass-official/vendor/assets/stylesheets/bootstrap';
and have no more issues. it compiles to main.css

Rails 3.1 Asset Pipeline manifest file won't pick up javascripts

I have a whole MESS of javascripts in vendor/assets/javascripts. In my app/assets/javascripts/application.js file, I have the directive:
//= require_tree .
Does that only reference the current app/assets/javascripts directory, and not lib/assets or vendor/assets?
If I explicitly include the javascripts, it works. I just don't really want to do that if I don't have to.
Is there something I am missing that will allow the assets pipeline to be able to serve up assets from outside the app directory (lib and vendor) automatically?
require_tree only pulls in assets that are under the application.js file.
lib/assets and vendor/assets are already included in the lookup paths for the pipeline (refer this code).
You can include these vendored files by using a second manifest.
Go to vendor/assets/javascripts and create a file called misc_vendor.js
Inside that add put a require_tree directive.
Then refer to that file from your application.js manifest:
require misc_vendor
If you have any issues because of loading order you can manually require the vendor files in the order you need instead of using require_tree.
As part of the conversion to the pipeline it may be a good chance to clean up things! :-)
Also, you can do it without a second manifest like this:
//= require_tree ../../../vendor/assets/javascripts/.
The path should be relative to the 'app/assets/javascripts/application.js' manifest file.
You need to extend path in application.rb file like this.
config.assets.paths << "#{Rails.root}/vendor/assets/some file name"
Refer this Guide for more details

Automatically create new folder under images on module installation

Is it possible for a Joomla extension to automatically create a folder (event) under images -> /images/events/ when the user installs the extension inside Joomla Administrator ?
Inside your component's xml file you will need to add in the following attribute:
<installfile>install.componentname.php</installfile>
replace with the name of your component, this can be added just underneath the description attribute of your components install xml file.
Once this has been added you will need to create a file called "install.componentname.php", again replace componentname with the name of your component.
Inside this file add the following:
<?php
// no direct access
defined('_JEXEC') or die('Restricted Access');
// import joomla's filesystem classes
jimport('joomla.filesystem.folder');
// create a folder inside your images folder
if(JFolder::create(JPATH_ROOT.DS.'images'.DS.'events')) {
echo "Folder created successfully";
} else {
echo "Unable to create folder";
} ?>
Package this up and install, the install..php file should be at the top level of your zip archive. Lastly you will need to add this file to your components file list, just after the attribute add the following line:
<files>
<filename>install.componentname.php</filename>
</files>
If the folder is created successfully, it will say Folder created successfully.
you can specify a custom php script to be run at extension installation in your extension's manifest file [1, 2]. this script could create your folder /images/events/.
there are some differences in the installer between joomla 1.5 and 1.6:
1.5
you can only do this for components, not for modules or plugins
to specify your custom script, you use the <installfile/> section of the manifest file
1.6
besides for components, you can use a custom install script for modules and plugins, too
use the <scriptfile/> section of the manifest file
[...] i see another answer has been posted. have a look at it for 1.5; for 1.6, use <scriptfile/> and have a look at http://docs.joomla.org/Developers , especially http://docs.joomla.org/How_to_use_the_filesystem_package . the actual creation of the folder is left as an exercise for the reader.
<!-- Site Main Media File Copy Section -->
<media destination="com_helloworld">
<filename>image.png</filename>
<filename>flash.swf</filename>
</media>
http://docs.joomla.org/Components:xml_installfile

Resources