Allow attributes

This commit is contained in:
Dennis Eichhorn 2019-04-01 21:54:26 +02:00
parent 905d04957d
commit 1c4dd55396

View File

@ -147,9 +147,9 @@ class Head implements RenderableInterface
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function addAsset(int $type, string $uri) : void public function addAsset(int $type, string $uri, array $attributes = []) : void
{ {
$this->assets[$uri] = $type; $this->assets[$uri] = ['type' => $type, 'attributes' => $attributes];
} }
/** /**
@ -299,16 +299,22 @@ class Head implements RenderableInterface
*/ */
public function renderAssets() : string public function renderAssets() : string
{ {
$asset = ''; $rendered = '';
foreach ($this->assets as $uri => $type) { foreach ($this->assets as $uri => $asset) {
if ($type === AssetType::CSS) { if ($asset['type'] === AssetType::CSS) {
$asset .= '<link rel="stylesheet" type="text/css" href="' . $uri . '">'; $rendered .= '<link rel="stylesheet" type="text/css" href="' . $uri . '">';
} elseif ($type === AssetType::JS) { } elseif ($asset['type'] === AssetType::JS) {
$asset .= '<script src="' . $uri . '"></script>'; $rendered .= '<script src="' . $uri . '"';
foreach ($asset['attributes'] as $key => $attribute) {
$rendered .= ' ' . $key . '="' . $attribute . '"';
}
$rendered .='></script>';
} }
} }
return $asset; return $rendered;
} }
/** /**
@ -320,13 +326,19 @@ class Head implements RenderableInterface
*/ */
public function renderAssetsLate() : string public function renderAssetsLate() : string
{ {
$asset = ''; $rendered = '';
foreach ($this->assets as $uri => $type) { foreach ($this->assets as $uri => $asset) {
if ($type === AssetType::JSLATE) { if ($asset['type']=== AssetType::JSLATE) {
$asset .= '<script src="' . $uri . '"></script>'; $rendered .= '<script src="' . $uri . '"';
foreach ($asset['attributes'] as $key => $attribute) {
$rendered .= ' ' . $key . '="' . $attribute . '"';
}
$rendered .='></script>';
} }
} }
return $asset; return $rendered;
} }
} }