PHPackages                             ucscode/uss-element - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. ucscode/uss-element

ActiveLibrary[Templating &amp; Views](/categories/templating)

ucscode/uss-element
===================

UssElement is a PHP library for building and manipulating HTML elements programmatically.

3.7.1(1y ago)41886MITPHPPHP &gt;=8.2

Since Jan 2Pushed 1y ago1 watchersCompare

[ Source](https://github.com/ucscode/uss-element)[ Packagist](https://packagist.org/packages/ucscode/uss-element)[ RSS](/packages/ucscode-uss-element/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (18)Used By (6)

Uss Element
===========

[](#uss-element)

A simple, lightweight, standalone PHP library for programmatically creating and manipulating HTML elements. It simplifies the process of working with HTML structures and DOM elements, offering functionality similar to [DOMDocument](https://www.php.net/manual/en/class.domdocument.php) but with reduced boilerplate and enhanced ease of use.

With UssElement, you can effortlessly create DOM nodes, set attributes, set innerHtml, use querySelector, modify element classlist etc, and generate (or render) HTML strings with ease.

### Why Uss Element?

[](#why-uss-element)

UssElement is designed to simplify and streamline the process of working with HTML elements in PHP. If (like me) you've ever been frustrated by the complexity of PHP's `DOMDocument` or found yourself writing repetitive, cumbersome code just to manipulate HTML structures, UssElement is the solution you’ve been waiting for.

This standalone library takes care of the heavy lifting, reducing boilerplate code and eliminates the need for complex XPath queries, offering a simple, intuitive API for tasks like creating elements, setting inner HTML, and selecting elements using CSS selectors.

The library is lightweight, fast, and easy to integrate into any project, making it perfect for both small and large-scale applications.

### Key Features:

[](#key-features)

- Create HTML elements using PHP code.
- More inspired by [Javascript DOM](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) than PHP DOMDocument
- Set element attributes, such as class names and IDs.
- Define inner HTML content for elements.
- Generate or render HTML strings with the `NodeInterface::render()` method.
- Boost your productivity by simplifying HTML generation in PHP.
- Reducing Complexity
- Providing an Intuitive API
- Encouraging Dependency-Free Development
- Efficiency in Common DOM Tasks
- Flexibility and Extensibility
- Improving Developer Experience
- Encoding element to Json format
- Decoding element from json format

### Prerequisite

[](#prerequisite)

- PHP &gt;= 8.2

### Installation (Composer)

[](#installation-composer)

You can include UssElement library in your project using Composer:

```
composer require ucscode/uss-element
```

### Getting Started:

[](#getting-started)

- Instantiate the UssElement class with the desired HTML element type (e.g., `NodeNameEnum::NODE_DIV`).
- Use UssElement methods to set attributes and content.
- Generate HTML strings with `NodeInterface::render()` for seamless integration into your web pages.

### Creating Elements

[](#creating-elements)

You can create elements by instantiating the type of node

```
use Ucscode\UssElement\Node\ElementNode;

$element = new ElementNode('div');
```

If you prefer, you can use the `NodeNameEnum` enum

```
use Ucscode\UssElement\Node\ElementNode;
use Ucscode\UssElement\Enums\NodeNameEnum;

$element = new ElementNode(NodeNameEnum::NODE_DIV);
```

You can also create an element and set their attributes at the point of instantiation:

```
$span = new ElementNode('span', [
  'id' => 'short-cut',
  'class' => 'to set',
  'data-what' => 'attributes'
]);
```

You can use many available methods to manipulate the DOM.

A summary of these methods are provided in the following sections:

- [NodeInterface methods](#nodeinterface-methods)
- [ElementInterface methods](#elementinterface-methods)
- [TextNode methods](#textnode-methods)

```
$element->appendChild($span);
```

```
$element->getNextSibling();
```

```
$element->getChild(0)
  ->setAttribute('data-name', 'Ucscode')
  ->setAttribute('title', 'Uss Element')
;
```

### Traversing Elements

[](#traversing-elements)

Use the `querySelector()` or `querySelectorAll()` method to select elements based on CSS selectors:

```
$element->querySelector('.to.set[data-what=attributes]'); // Returns the  element
```

You can also retrieve an element by other methods such as:

- `getElementsByClassName`
- `getElementsByTagName`

```
$element->getElementsByClassName('.set'); // Returns the  element
```

### Inner HTML

[](#inner-html)

- You can easily set the inner HTML content of an element using the `setInnerHtml()` method:
- You can also get inner HTML of an element using `getInnerHTML()` method:

```
$element->setInnerHtml('This is a paragraph inside a div.');
```

### Loading HTML

[](#loading-html)

You can convert an HTML string to `NodeList` containing all elements using the `HtmlLoader` class:

```
use Ucscode\UssElement\Parser\Translator\HtmlLoader;

// An example HTML document:
$html = count(); // Returns the number of direct nodes (1 in this case)
$htmlLoader->getNodeList()->first; // HTML ElementNode
```

You can also load framents

```
use Ucscode\UssElement\Parser\Translator\HtmlLoader;

$html =
