Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/configurable assets attributes #46

Merged
merged 2 commits into from
Sep 5, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 37 additions & 9 deletions system/src/Grav/Common/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,10 @@ public function addJs($asset, $priority = 10, $pipeline = true)
/**
* Build the CSS link tags.
*
* @param array $attributes
* @return string
*/
public function css()
public function css($attributes = [])
{
if( ! $this->css)
return null;
Expand All @@ -271,31 +272,32 @@ public function css()
usort($this->css, function ($a, $b) {return $a['priority'] - $b['priority'];});
$this->css = array_reverse($this->css);


$attributes = $this->attributes(array_merge([ 'type' => 'text/css', 'rel' => 'stylesheet' ], $attributes));

$output = '';
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) {
$output .= '<link type="text/css" rel="stylesheet" href="'.$file['asset'].'" />'."\n";
$output .= '<link href="'.$file['asset'].'"'.$attributes.' />'."\n";
}
return $output;
}


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;
}

/**
* Build the JavaScript script tags.
*
* @param array $attributes
* @return string
*/
public function js()
public function js($attributes = [])
{
if( ! $this->js)
return null;
Expand All @@ -304,22 +306,48 @@ public function js()
usort($this->js, function ($a, $b) {return $a['priority'] - $b['priority'];});
$this->js = array_reverse($this->js);

$attributes = $this->attributes(array_merge([ 'type' => 'text/javascript' ], $attributes));

$output = '';
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) {
$output .= '<script type="text/javascript" src="'.$file['asset'].'"></script>'."\n";
$output .= '<script src="'.$file['asset'].'"'.$attributes.' ></script>'."\n";
}
return $output;
}


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;
}

/**
* 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.
*
Expand Down