Skip to content

Instantly share code, notes, and snippets.

@hellozach
Last active August 29, 2015 14:15
Show Gist options
  • Save hellozach/9d8dbed4e69a59f7c4b8 to your computer and use it in GitHub Desktop.
Save hellozach/9d8dbed4e69a59f7c4b8 to your computer and use it in GitHub Desktop.
PHP Enqueue Class
<?php
/**
* Enqueue JavaScript and CSS Styles.
*
* @version 1.0 2015/02/18
* @author Zach Johnson <[email protected]>
*/
class Enqueue {
/** Start Config area */
private $path_js = '/full/path/to/js/folder/';
private $path_css = '/full/path/to/css/folder/';
/** End Config area */
private $scripts = array();
private $styles = array();
/**
* Add a resource to the queue.
*
* @param string $type The type of resource.
* @param string $source The filename of the resource, excluding file extension and folder path.
*/
public function add($type, $source) {
switch( $type ) {
case 'script' :
case 'js' :
$this->scripts[] = $source;
break;
case 'style' :
case 'css' :
$this->styles[] = $source;
break;
}
}
/**
* Loads the resources.
*
* @param string $type Define which resources to load.
*/
public function load($type) {
$output = '';
switch( $type ) {
case 'script' :
case 'scripts' :
$load = 'scripts';
$format = '<script src="' . $this->path_js . '%s.js"></script>' . PHP_EOL;
break;
case 'style' :
case 'styles' :
$load = 'styles';
$format = '<link rel="stylesheet" href="' . $this->path_css . '%s.css">' . PHP_EOL;
break;
}
if( ! isset($this->$load) )
return false;
$uniqueEnqueue = array_unique($config->enqueued[$load]);
foreach( $uniqueEnqueue as $enqueue ) {
$output .= sprintf($format,$enqueue);
}
echo $output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment