Skip to content

Instantly share code, notes, and snippets.

@krypton
Created October 18, 2012 10:13
Show Gist options
  • Save krypton/3910865 to your computer and use it in GitHub Desktop.
Save krypton/3910865 to your computer and use it in GitHub Desktop.
Assets include
<?php
define('_ROOT', dirname(realpath(__FILE__)));
function assets_include_tag($assets, $tmpl, $options)
{
if (empty($assets)) {
return false;
}
$assetOptions = '';
$assetOutput = array();
foreach ($options as $key => $value) {
$assetOptions .= " {$key}=\"{$value}\"";
}
foreach ($assets as $asset) {
$filepath = _ROOT . "/{$asset}";
if (is_file($filepath)) {
array_push($assetOutput, sprintf($tmpl, "{$asset}?v=" . filemtime($filepath), $assetOptions));
}
}
return implode(PHP_EOL, $assetOutput) . PHP_EOL;
}
function javascript_include_tag($assets, $options = array())
{
$default = array('type' => 'text/javascript');
return assets_include_tag($assets, '<script src="%s"%s></script>', array_merge($default, $options));
}
function stylesheet_link_tag($assets, $options = array())
{
$default = array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'all');
return assets_include_tag($assets, '<link href="%s"%s />', array_merge($default, $options));
}
// Examples of usage:
// <?php echo stylesheet_link_tag(array('stylesheets/all.css')) ?>
// <?php echo javascript_include_tag(array('javascripts/all.js')) ?>
// <?php echo javascript_include_tag(array('javascripts/search.js'), array('charset' => 'UTF-8')) ?>
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment