A PHP Library for Effortless HTML Generation (Package From User Synthetics)
UssElement is a powerful PHP library designed to simplify the process of building and manipulating HTML elements in your web applications. With UssElement, you can effortlessly create DOM nodes, set attributes, define content, and generate HTML strings with ease. Streamline your PHP-based web development workflow and save time when constructing dynamic and interactive web pages.
- Create HTML elements using PHP code.
- Set element attributes, such as class names and IDs.
- Define inner HTML content for elements.
- Generate HTML strings with the
UssElement::getHTML()
method. - Boost your productivity by simplifying HTML generation in PHP.
You can include UssElement in your project using Composer:
composer require ucscode/uss-element
- Instantiate the UssElement class with the desired HTML element type (e.g.,
UssElement::NODE_DIV
). - Use UssElement methods to set attributes and content.
- Generate HTML strings with
UssElement::getHTML()
for seamless integration into your web pages.
use Ucscode\UssElement\UssElement;
// Create Elements
$div = new UssElement(UssElement::NODE_DIV);
$span = new UssElement(UssElement::NODE_SPAN);
// Modify Elements
$div->setAttribute('class', 'container');
$span->setAttribute('style', "color: red;");
$span->setContent("Hello world!");
// Organize Element
$div->appendChild($span);
// Generate and output HTML string
echo $div->getHTML(true); // true to indent html output
But why use this over the default PHP DOMDocument
?
UssElement not only provides out of the box methods and concept but is prepared for ultimate future increase. The default DOMDocument uses methods that are similar to that of Javascript and nothing more. There are also maximized limitation like being unable to directly insert innerHTML
into your document context.
With UssElement, you can do all that and even more. For example, you can set the visibility of an element to render with other element or not.
$div->appendChild($span);
$span->setInvisible(true);
In the above example, span is a child of div and always will be unless removed. However, when the content is rendered in HTML, the span element will be absent, giving you the ability to control the element visiblity without actually removing the element.
Method | Returns | Description |
---|---|---|
isVoid() |
bool |
Indicates whether the element should have a closing tag or not (true means no closing tag). |
hasAttribute($attr) |
bool |
Checks if an attribute exists on the element. |
hasAttributeValue($attr, $value) |
bool |
Checks if an attribute has a particular value. |
getAttribute($attr) |
?string |
Gets the value of an attribute. |
addAttributeValue($attr, $value) |
self |
Appends a value to an attribute. |
removeAttributeValue($attr, $value) |
self |
Removes a value from an attribute. |
removeAttribute($attr) |
self |
Removes an attribute from the element. |
setContent($content) |
self |
Sets the inner HTML content of the element. |
hasContent() |
bool |
Checks if the element has inner HTML content. |
getContent() |
string |
Gets the inner HTML content of the element. |
appendChild($child) |
void |
Appends a child element to the current element. |
prependChild($child) |
void |
Prepends a child element to the current element. |
insertBefore($child, $refNode) |
void |
Inserts a child element before a specified reference element. |
insertAfter($child, $refNode) |
void |
Inserts a child element after a specified reference element. |
replaceChild($child, $refNode) |
void |
Replaces a child element with another element. |
firstChild() |
?UssElement |
Returns the first child element of the current element. |
lastChild() |
?UssElement |
Returns the last child element of the current element. |
getChild($index) |
?UssElement |
Returns a child element at a specified index. |
removeChild($child) |
void |
Removes a child element from the current element. |
getHTML($indent = false) |
string |
Generates an HTML string representation of the element and its children. |
If you find UssElement helpful and would like to support its development and maintenance, you can make a donation.
Your contribution helps ensure the continued improvement and sustainability of this project.
- Bitcoin: You can also contribute with Bitcoin by sending to our Bitcoin wallet address:
bc1q22zymcsq9t7m9fdwau3dqcpme2szvgnzkqyjza
.
Note: UssElement is an open-source project, and contributions are voluntary. Donations are not tax-deductible.