Skip to content

Instantly share code, notes, and snippets.

@phproberto
Last active December 11, 2015 14:38
Show Gist options
  • Save phproberto/4615320 to your computer and use it in GitHub Desktop.
Save phproberto/4615320 to your computer and use it in GitHub Desktop.
Joomla! overridable asset loader

Joomla! overridable media asset loader

Asset loader based on @dongilbert asset loader ( https://gist.github.com/4205674 )

Stylesheets

ELHtml::asset('style.css');

Checks in this order (when called inside com_helloworld component):

/templates/MY_TEMPLATE/css/com_helloworld/style.css  
/media/com_helloworld/css/style.css  

Javascript

ELHtml::asset('slide.js', 'mod_menu');

Checks in this order:

/templates/MY_TEMPLATE/js/mod_menu/slide.js  
/media/mod_menu/js/slide.js   

Images

echo ELHtml::asset('search.png', 'mod_product_search', array('alt' => 'This is the alt text'));

Checks in this order:

/templates/MY_TEMPLATE/images/mod_product_search/search.png  
/media/mod_product_search/images/search.png 
<?php
defined('_JEXEC') or die;
class ELHtml 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.
* @param array $attribs Extra attribs array
*
* @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, $attribs = array())
{
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':
return self::stylesheet($toLoad, $attribs, true, false);
break;
case 'JS':
return self::script($toLoad, false, true);
break;
case 'GIF':
case 'JPG':
case 'JPEG':
case 'PNG':
case 'BMP':
$alt = null;
if (isset($attribs['alt']))
{
$alt = $attribs['alt'];
unset($attribs['alt']);
}
return self::image($toLoad, $alt, $attribs, true);
break;
default:
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment