Skip to content

Instantly share code, notes, and snippets.

@gitbuh
Created April 5, 2012 21:11
Show Gist options
  • Save gitbuh/2314192 to your computer and use it in GitHub Desktop.
Save gitbuh/2314192 to your computer and use it in GitHub Desktop.
Simple static resource loader for JS / CSS files
<?php
/**
Simple static resource loader for JS / CSS files.
*/
class ResourceLoader {
public static $css;
public static $js;
/**
Require a CSS file.
@param string $path
Domain-relative resource path.
@param array $attribs
Optional HTML attributes.
*/
public static function requireCSS ($path, $attribs=null) {
self::$css[$path] = $attribs ? $attribs : array();
}
/**
Require a JS file.
@param string $path
Domain-relative resource path.
@param array $attribs
Optional HTML attributes.
*/
public static function requireJS ($path, $attribs=null) {
self::$js[$path] = $attribs ? $attribs : array();
}
/**
Get HTML tags to include resorces.
@return string
*/
public static function getTags () {
$out = '';
foreach (self::$css as $k => $v) {
$out .= "<link rel='stylesheet' type='text/css' src='$k'";
foreach ($v as $attribute => $value) {
$out .= " $attribute='$value'";
}
$out .= " />\n";
}
foreach (self::$js as $k => $v) {
$out .= "<script type='text/javascript' src='$k'";
foreach ($v as $attribute => $value) {
$out .= " $attribute='$value'";
}
$out .= "></script>\n";
}
return $out;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment