Merge pull request #46 from rindeal/feature/configurable-assets-attributes

Feature/configurable assets attributes
This commit is contained in:
Djamil Legato
2014-09-05 14:51:34 -07:00

View File

@@ -260,9 +260,10 @@ class Assets
/** /**
* Build the CSS link tags. * Build the CSS link tags.
* *
* @param array $attributes
* @return string * @return string
*/ */
public function css() public function css($attributes = [])
{ {
if( ! $this->css) if( ! $this->css)
return null; return null;
@@ -271,21 +272,21 @@ class Assets
usort($this->css, function ($a, $b) {return $a['priority'] - $b['priority'];}); usort($this->css, function ($a, $b) {return $a['priority'] - $b['priority'];});
$this->css = array_reverse($this->css); $this->css = array_reverse($this->css);
$attributes = $this->attributes(array_merge([ 'type' => 'text/css', 'rel' => 'stylesheet' ], $attributes));
$output = ''; $output = '';
if($this->css_pipeline) { if($this->css_pipeline) {
$output .= '<link type="text/css" rel="stylesheet" href="'.$this->pipeline(CSS_ASSET).'" />'."\n"; $output .= '<link href="'.$this->pipeline(CSS_ASSET).'"'.$attributes.' />'."\n";
foreach ($this->css_no_pipeline as $file) { foreach ($this->css_no_pipeline as $file) {
$output .= '<link type="text/css" rel="stylesheet" href="'.$file['asset'].'" />'."\n"; $output .= '<link href="'.$file['asset'].'"'.$attributes.' />'."\n";
} }
return $output; return $output;
} }
foreach($this->css as $file) foreach($this->css as $file)
$output .= '<link type="text/css" rel="stylesheet" href="'.$file['asset'].'" />'."\n"; $output .= '<link href="'.$file['asset'].'"'.$attributes.' />'."\n";
return $output; return $output;
} }
@@ -293,9 +294,10 @@ class Assets
/** /**
* Build the JavaScript script tags. * Build the JavaScript script tags.
* *
* @param array $attributes
* @return string * @return string
*/ */
public function js() public function js($attributes = [])
{ {
if( ! $this->js) if( ! $this->js)
return null; return null;
@@ -304,22 +306,48 @@ class Assets
usort($this->js, function ($a, $b) {return $a['priority'] - $b['priority'];}); usort($this->js, function ($a, $b) {return $a['priority'] - $b['priority'];});
$this->js = array_reverse($this->js); $this->js = array_reverse($this->js);
$attributes = $this->attributes(array_merge([ 'type' => 'text/javascript' ], $attributes));
$output = ''; $output = '';
if($this->js_pipeline) { if($this->js_pipeline) {
$output .= '<script type="text/javascript" src="'.$this->pipeline(JS_ASSET).'"></script>'."\n"; $output .= '<script src="'.$this->pipeline(JS_ASSET).'"'.$attributes.' ></script>'."\n";
foreach ($this->js_no_pipeline as $file) { foreach ($this->js_no_pipeline as $file) {
$output .= '<script type="text/javascript" src="'.$file['asset'].'"></script>'."\n"; $output .= '<script src="'.$file['asset'].'"'.$attributes.' ></script>'."\n";
} }
return $output; return $output;
} }
foreach($this->js as $file) foreach($this->js as $file)
$output .= '<script type="text/javascript" src="'.$file['asset'].'"></script>'."\n"; $output .= '<script src="'.$file['asset'].'"'.$attributes.' ></script>'."\n";
return $output; return $output;
} }
/**
* Build an HTML attribute string from an array.
*
* @param array $attributes
* @return string
*/
protected function attributes(array $attributes){
$html = '';
foreach ( $attributes as $key => $value)
{
// For numeric keys we will assume that the key and the value are the same
// as this will convert HTML attributes such as "required" to a correct
// form like required="required" instead of using incorrect numerics.
if (is_numeric($key)) $key = $value;
if (is_array($value)) $value = implode(' ', $value);
$element = $key.'="'.htmlentities($value, ENT_QUOTES, 'UTF-8', false).'"';
$html .= ' '.$element;
}
return $html;
}
/** /**
* Add/replace collection. * Add/replace collection.
* *