Html & Dom Builder
This is a convenience class to create XML and HTML element in an OO way.
DomElement
DomElement and DomElements is use to create XML elements.
use Windwalker\Dom\DomElement;
$attrs = array('id' => 'foo', 'class' => 'bar');
echo $dom = (string) new DomElement('field', 'Content', $attrs);
Output:
<field id="foo" class="bar">Content</field>
Add Children
use Windwalker\Dom\DomElement;
$attrs = array('id' => 'foo', 'class' => 'bar');
$content = array(
new DomElement('option', 'Yes', array('value' => 1)),
new DomElement('option', 'No', array('value' => 0))
);
echo $dom = (string) new DomElement('field', $content, $attrs);
The output will be:
<field id="foo" class="bar">
<option value="1">Yes</option>
<option value="0">No</option>
</field>
HtmlElement
HtmlElement is use to create HTML elements, some specific tags will force to unpaired.
use Windwalker\Dom\HtmlElement;
$attrs = array(
'class' => 'btn btn-mini',
'onclick' => 'return false;'
);
$html = (string) new HtmlElement('button', 'Click', $attrs);
Then we will get this HTML:
<button class="btn btn-mini" onclick="return false;">Click</button>
Get Attributes by Array Access
$class = $html['class'];
DomElements & HtmlElements
It is a collection of HtmlElement set.
$html = new HtmlElements(
array(
new HtmlElement('p', $content, $attrs),
new HtmlElement('div', $content, $attrs),
new HtmlElement('a', $content, $attrs)
)
);
echo $html;
OR we can iterate it:
foreach ($html as $element)
{
echo $element;
}
Attributes
$html = new HtmlElement('input', array(
'data-string' => 'string',
'data-empty' => '',
'data-true' => true,
'data-false' => false,
'data-null' => null,
// Special attributes
'checked' => 'checked',
'disabled' => true,
'readonly' => false
));
echo $html;
Result
<input data-string="string" data-empty="" data-true checked="checked" disabled="disabled">
Formatter
DomFormatter
and HtmlFormatter
will help us format XML
/ HTML
string.
$xml = '<field id="foo" class="bar"><option value="1">Yes</option><option value="0">No</option></field>';
DomFormatter::format($xml);
Result
<field id="foo" class="bar">
<option value="1">Yes</option>
<option value="0">No</option>
</field>
HtmlFormatter
will convert some tags to unpaired element, e.g. <img>
.
Found a typo? Help us improve this document.
This document is for Windwalker Joomla RAD, if you are finding Windwalker PHP framework, please see: Windwalker Framework