Skip to content

Internals Render Render

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

render\Render

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/render/Render.php · Snapshot 8411f4287706.

Rendering combines package views, Blade compilation, DOM processing and the renderer.ready event. The upstream BladeOne engine is excluded from the exhaustive framework reference; AWT integration is documented separately.

Imports and dependencies

use DOMDocument;
use DOMXPath;
use Error;
use event\EventDispatcher;
use Exception;
use render\events\RenderReadyEvent;
use render\TemplateEngine\BladeOne;
use Throwable;

Render

Declaration and architectural role

class Render

Source description (behavioral corrections are recorded in Known discrepancies):

The Render class provides tools to manipulate HTML documents, load templates, and manage dynamic content rendering. It uses DOM manipulation and integrates an event dispatcher for handling specific events during the render process.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$routes public array $routes = []; Declared extension/state field; no own method directly consumes it in this source file.
$localAssetPath public string $localAssetPath; Declared extension/state field; no own method directly consumes it in this source file.
$bundle public array $bundle; Used by addToBundle(), createBundleObjects().
$dom protected DOMDocument $dom; Used by createDocument(), loadTemplate(), getDom(), replaceElementsByClassname(), insertAfterByClassname(), insertBeforeByClassname(), insertIntoByClassname().
$eventDispatcher public EventDispatcher $eventDispatcher; Declared extension/state field; no own method directly consumes it in this source file.
$packageName public string $packageName; Declared extension/state field; no own method directly consumes it in this source file.

Method index

Method Signature
createDocument public function createDocument(string $html = "<html lang=''></html>"): DOMDocument
loadTemplate protected function loadTemplate(): void
getDom public function getDom(): DOMDocument
replaceElementsByClassname public function replaceElementsByClassname(string $classname, DOMDocument $newElement): void
insertAfterByClassname public function insertAfterByClassname(string $classname, DOMDocument $html): void
insertBeforeByClassname public function insertBeforeByClassname(string $classname, DOMDocument $html): void
insertIntoByClassname public function insertIntoByClassname(string $classname, DOMDocument $html): void
addToBundle public function addToBundle(string $key, string $value): void
createBundleObjects protected function createBundleObjects(): void

createDocument

Source line 33

public function createDocument(string $html = "<html lang=''></html>"): DOMDocument

Parameters

Parameter Type, modifiers and default Purpose
$html string $html = "<html lang=''></html>" The HTML content to load into the DOMDocument. Defaults to a basic HTML structure.

Creates a new DOMDocument instance and loads HTML content into it.

@param string $html The HTML content to load into the DOMDocument. Defaults to a basic HTML structure. @return DOMDocument The created and loaded DOMDocument instance.

State writes: $this->dom.

Return expressions (including local callbacks): $this->dom.

Failure handling catches Exception.

External effects include die.

Implementation, validation guards and interaction order
/**
     * Creates a new DOMDocument instance and loads HTML content into it.
     *
     * @param string $html The HTML content to load into the DOMDocument. Defaults to a basic HTML structure.
     * @return DOMDocument The created and loaded DOMDocument instance.
     */
    public function createDocument(string $html = "<html lang=''></html>"): DOMDocument
    {
        try {
            $this->dom = new DOMDocument();
            $this->dom->formatOutput = true;
            $this->dom->loadHTML($html);
            return $this->dom;
        } catch (Exception $exception) {
            die("AWT Renderer: Failed to create document.");
        }
    }

loadTemplate

Source line 51

protected function loadTemplate(): void

Loads a template from an HTML document if it extends another template. Uses regex to find the "@extends" directive and merges the content with the base template.

@throws Exception If the template cannot be loaded or extended.

Delegates to createDocument().

Implementation, validation guards and interaction order
/**
     * Loads a template from an HTML document if it extends another template.
     * Uses regex to find the "@extends" directive and merges the content with the base template.
     *
     * @throws Exception If the template cannot be loaded or extended.
     */
    protected function loadTemplate(): void
    {
        $htmlContent = $this->dom->saveHTML();
        $this->createDocument($htmlContent);
    }

getDom

Source line 58

public function getDom(): DOMDocument

Return expressions (including local callbacks): $this->dom.

Implementation, validation guards and interaction order
public function getDom(): DOMDocument
    {
        return $this->dom;
    }

replaceElementsByClassname

Source line 69

public function replaceElementsByClassname(string $classname, DOMDocument $newElement): void

Parameters

Parameter Type, modifiers and default Purpose
$classname string $classname The class name to search for in the current DOM.
$newElement DOMDocument $newElement The new element to replace the matching elements.

Replaces all elements in the DOM that match the given class name with a new DOM element.

@param string $classname The class name to search for in the current DOM. @param DOMDocument $newElement The new element to replace the matching elements.

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
/**
     * Replaces all elements in the DOM that match the given class name with a new DOM element.
     *
     * @param string $classname The class name to search for in the current DOM.
     * @param DOMDocument $newElement The new element to replace the matching elements.
     */
    public function replaceElementsByClassname(string $classname, DOMDocument $newElement): void
    {
        $xpath = new \DOMXPath($this->dom);
        $elements = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");

        foreach ($elements as $element) {
            $importedNode = $this->dom->importNode($newElement->documentElement, true);
            $element->parentNode->replaceChild($importedNode, $element);
        }
    }

insertAfterByClassname

Source line 86

public function insertAfterByClassname(string $classname, DOMDocument $html): void

Parameters

Parameter Type, modifiers and default Purpose
$classname string $classname The class name to search for in the current DOM.
$html DOMDocument $html The new element to insert after the matching elements.

Inserts a new element in the DOM after the elements that match the given class name.

@param string $classname The class name to search for in the current DOM. @param DOMDocument $html The new element to insert after the matching elements.

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
/**
     * Inserts a new element in the DOM after the elements that match the given class name.
     *
     * @param string $classname The class name to search for in the current DOM.
     * @param DOMDocument $html The new element to insert after the matching elements.
     */
    public function insertAfterByClassname(string $classname, DOMDocument $html): void
    {
        $xpath = new \DOMXPath($this->dom);
        $elements = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");

        foreach ($elements as $element) {
            $newNode = $this->dom->importNode($html->documentElement, true);

            if ($element->nextSibling) {
                $element->parentNode->insertBefore($newNode, $element->nextSibling);
            } else {
                $element->parentNode->appendChild($newNode);
            }
        }
    }

insertBeforeByClassname

Source line 108

public function insertBeforeByClassname(string $classname, DOMDocument $html): void

Parameters

Parameter Type, modifiers and default Purpose
$classname string $classname The class name to search for in the current DOM.
$html DOMDocument $html The new element to insert before the matching elements.

Inserts a new element in the DOM before the elements that match the given class name.

@param string $classname The class name to search for in the current DOM. @param DOMDocument $html The new element to insert before the matching elements.

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
/**
     * Inserts a new element in the DOM before the elements that match the given class name.
     *
     * @param string $classname The class name to search for in the current DOM.
     * @param DOMDocument $html The new element to insert before the matching elements.
     */
    public function insertBeforeByClassname(string $classname, DOMDocument $html): void
    {
        $xpath = new DOMXPath($this->dom);
        $elements = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");

        foreach ($elements as $element) {
            $newNode = $this->dom->importNode($html->documentElement, true);
            $element->parentNode->insertBefore($newNode, $element);
        }
    }

insertIntoByClassname

Source line 125

public function insertIntoByClassname(string $classname, DOMDocument $html): void

Parameters

Parameter Type, modifiers and default Purpose
$classname string $classname The class name to search for in the current DOM.
$html DOMDocument $html The new element to append to the matching elements.

Appends a new element into elements that match the given class name.

@param string $classname The class name to search for in the current DOM. @param DOMDocument $html The new element to append to the matching elements.

Explicit exceptions: \RuntimeException, \InvalidArgumentException. The guards in the implementation below show when each is raised; exceptions from collaborators also propagate unless caught.

Implementation, validation guards and interaction order
/**
     * Appends a new element into elements that match the given class name.
     *
     * @param string $classname The class name to search for in the current DOM.
     * @param DOMDocument $html The new element to append to the matching elements.
     */
    public function insertIntoByClassname(string $classname, DOMDocument $html): void
    {
        if (!isset($this->dom)) {
            throw new \RuntimeException('DOMDocument instance is not initialized.');
        }

        $classname = ltrim($classname, '.'); // Remove leading `.` if present

        if (!preg_match('/^[a-zA-Z0-9-_]+$/', $classname)) {
            throw new \InvalidArgumentException("Invalid class name: '$classname'.");
        }

        if ($html->documentElement === null) {
            throw new \InvalidArgumentException('The provided DOMDocument has no valid documentElement.');
        }

        $xpath = new DOMXPath($this->dom);

        // Match the exact class name
        $elements = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' {$classname} ')]");

        foreach ($elements as $element) {
            if ($element instanceof \DOMElement) {
                $newNode = $this->dom->importNode($html->documentElement, true);
                $element->appendChild($newNode);
            }
        }
    }

addToBundle

Source line 160

public function addToBundle(string $key, string $value): void

Parameters

Parameter Type, modifiers and default Purpose
$key string $key The key associated with the bundle item.
$value string $value The value of the bundle item.

Adds a key-value pair to the bundle. The bundle stores various data to be used in rendering.

@param string $key The key associated with the bundle item. @param string $value The value of the bundle item.

State writes: $this->bundle[$key].

Implementation, validation guards and interaction order
/**
     * Adds a key-value pair to the bundle. The bundle stores various data to be used in rendering.
     *
     * @param string $key The key associated with the bundle item.
     * @param string $value The value of the bundle item.
     */
    public function addToBundle(string $key, string $value): void
    {
        $this->bundle[$key] = $value;
    }

createBundleObjects

Source line 168

protected function createBundleObjects(): void

Converts the bundle key-value pairs into object properties for later use in rendering.

Computes or delegates the operation shown below; no explicit receiver property assignment or throw occurs in this method.

Implementation, validation guards and interaction order
/**
     * Converts the bundle key-value pairs into object properties for later use in rendering.
     */
    protected function createBundleObjects(): void
    {
        foreach ($this->bundle as $key => $bundle) {
            $this->{$key} = $bundle;
        }
    }

Related types

Clone this wiki locally