public
Last active

Joomla! overridable asset loader

  • Download Gist
examples.md
Markdown

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 
html.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?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;
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.