PHP code example of ucscode / uss-element

1. Go to this page and download the library: Download ucscode/uss-element library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ucscode / uss-element example snippets


use Ucscode\UssElement\Node\ElementNode;

$element = new ElementNode('div');

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

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

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

$element->appendChild($span);

$element->getNextSibling();

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

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

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

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

use Ucscode\UssElement\Parser\Translator\HtmlLoader;

// An example HTML document:
$html = <<< 'HERE'
  <html>
    <head>
      <title>TEST</title>
    </head>
    <body id='foo'>
      <h1>Hello World</h1>
      <p>This is a test of the HTML5 parser.</p>
    </body>
  </html>
HERE;

$htmlLoader = new HtmlLoader($html);

$htmlLoader->getNodeList()->count(); // Returns the number of direct nodes (1 in this case)
$htmlLoader->getNodeList()->first; // HTML ElementNode

use Ucscode\UssElement\Parser\Translator\HtmlLoader;

$html = <<< 'HERE'
  <h1>Hi there</h1>
  <p>Please enter your detail</p>
  <form name="my-form>
    <input name="username"/>
  </form>
HERE;

$htmlLoader = new HtmlLoader($html);

$htmlLoader->getNodeList()->count(); // Returns the number of direct nodes (3 in this case)

$htmlLoader->getNodeList()->get(0); // H1 ElementNode
$htmlLoader->getNodeList()->get(1); // P ElementNode
$htmlLoader->getNodeList()->get(2); // FORM ElementNode

$html = '<div class="container"><p>Hello, world!</p></div>';
$htmlLoader = new HtmlLoader($html);

// Access the root div element
$divElement = $htmlLoader->getNodeList()->get(0);

// Set inner HTML of the root element
$divElement->setInnerHtml('<i class="fa-icon"></i><h1 class="heading">New Heading</h1><br/>');

// Query the first paragraph within the container
$paragraph = $divElement->querySelector('p'); // null
$heading = $divElement->querySelector('h1.heading'); // H1 ElementNode

// Accessing the number of direct child nodes
echo $divElement->getChildNodes()->count(); // 3

echo $divElement->render();

echo $divElement->render(0);

$divElement->querySelector('.heading')->setVisible(false);

$divElement->render(0);

$divElement->getChildren()->count(); // 3

$element = new Element('x-widget', [
  ':vue-binder' => 'project'
]);

$element->render(); // <x-widget :vue-binder="project"></x-widget>

$element->setVoid(true);

$element->render(); // <x-widget :vue-binder="project"/>

$node->toJson(); // Node to JSON Serialization

(new NodeJsonEncoder($node))->encode();

(new NodeJsonDecoder($json))->decode(); // JSON to Node Deserilization

(new NodeJsonEncoder($node))->normalize(); // to array
(new NodeJsonDecoder($json))->normalize(); // to array