Created
April 5, 2012 21:11
-
-
Save gitbuh/2314192 to your computer and use it in GitHub Desktop.
Simple static resource loader for JS / CSS files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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