Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Last active October 13, 2015 13:57
Show Gist options
  • Save dongilbert/4205674 to your computer and use it in GitHub Desktop.
Save dongilbert/4205674 to your computer and use it in GitHub Desktop.
JHtml::asset()

Joomla Asset Management with JHtml

// To load a css file for a component, from within the component
EEHtml::asset('style.css');

// 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);
break;
case 'JS':
self::script($toLoad, false, true);
break;
case 'GIF':
case 'JPG':
case 'JPEG':
case 'PNG':
case 'BMP':
return self::image($toLoad, null, null, true);
break;
default:
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment