Skip to content

Instantly share code, notes, and snippets.

Last active October 13, 2015 13:57
What would you like to do?

Joomla Asset Management with JHtml

// To load a css file for a component, from within the component

// To load a js file for a module
EEHtml::asset('slide.js', 'mod_menu');

// To load an image for a module
echo EEHtml::asset('search.png', 'mod_product_search');
<?php defined('EE_PATH') or die;
abstract class EEHtml extends JHtml
* Includes assets from media directory, looking in the
* template folder for a style override to include.
* @param string $filename Path to file.
* @param string $extension Current extension name. Will auto detect component name if null.
* @return mixed False if asset type is unsupported, nothing if a css or js file, and a string if an image
public static function asset($filename, $extension = null)
if (is_null($extension))
$extension = array_pop(explode(DIRECTORY_SEPARATOR, JPATH_COMPONENT));
$toLoad = "$extension/$filename";
// Discover the asset type from the file name
$type = substr($filename, (strrpos($filename, '.') + 1));
switch (strtoupper($type))
case 'CSS':
self::stylesheet($toLoad, false, true, false);
case 'JS':
self::script($toLoad, false, true);
case 'GIF':
case 'JPG':
case 'JPEG':
case 'PNG':
case 'BMP':
return self::image($toLoad, null, null, true);
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment