ipl/Attributes: allow a prefix, helps with data-*

This commit is contained in:
Thomas Gelf 2018-02-20 14:36:01 +01:00
parent 344e6a8e59
commit 20bcbdccf7
2 changed files with 31 additions and 3 deletions

View File

@ -13,6 +13,9 @@ class Attributes
/** @var callable */
protected $callbacks = array();
/** @var string */
protected $prefix = '';
/**
* Attributes constructor.
* @param Attribute[] $attributes
@ -121,6 +124,8 @@ class Attributes
foreach ($attribute as $name => $value) {
$this->set($name, $value);
}
return $this;
} else {
return $this->setAttribute(new Attribute($attribute, $value));
}
@ -239,6 +244,20 @@ class Attributes
$parts[] = $attribute->render();
}
return ' ' . implode(' ', $parts);
$separator = ' ' . $this->prefix;
return $separator . implode($separator, $parts);
}
/**
* @param string $prefix
* @return $this
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
return $this;
}
}

View File

@ -135,7 +135,7 @@ abstract class BaseElement extends Html
return sprintf(
'<%s%s>%s</%s>',
$tag,
$this->attributes()->render(),
$this->renderAttributes(),
$content,
$tag
);
@ -143,11 +143,20 @@ abstract class BaseElement extends Html
return sprintf(
'<%s%s />',
$tag,
$this->attributes()->render()
$this->renderAttributes()
);
}
}
public function renderAttributes()
{
if ($this->attributes === null && empty($this->defaultAttributes)) {
return '';
} else {
return $this->attributes()->render();
}
}
public function wantsClosingTag()
{
// TODO: There is more. SVG and MathML namespaces