Skip to content

Internals View View

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

view\View

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/view/View.php · Snapshot 8411f4287706.

A View stores the template name and bundle, initially includes the view file, then compiles the Blade template on render(). Controllers inherit this behavior.

Imports and dependencies

use DOMDocument;
use Exception;
use render\events\RenderReadyEvent;
use render\Render;
use render\TemplateEngine\BladeOne;
use Throwable;

View

Declaration and architectural role

class View extends Render

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
$viewDirectory protected string $viewDirectory; @var string The directory where view files are stored.
$viewName public string $viewName; @var string The name of the view to be rendered.
$csrfTokens private ?\router\interface\ICsrfTokenManager $csrfTokens = null; Used by setCsrfTokenManager(), render().
$page private DOMDocument $page; @var DOMDocument The DOM representation of the rendered view page.

Method index

Method Signature
setCsrfTokenManager public function setCsrfTokenManager(\router\interface\ICsrfTokenManager $tokens): self
view public function view(array $data = []): View
getViewContent protected function getViewContent(): string
render public function render(): string

setCsrfTokenManager

Source line 25

public function setCsrfTokenManager(\router\interface\ICsrfTokenManager $tokens): self

Parameters

Parameter Type, modifiers and default Purpose
$tokens \router\interface\ICsrfTokenManager $tokens Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->csrfTokens.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function setCsrfTokenManager(\router\interface\ICsrfTokenManager $tokens): self
    {
        $this->csrfTokens = $tokens;
        return $this;
    }

view

Source line 45

public function view(array $data = []): View

Parameters

Parameter Type, modifiers and default Purpose
$data array $data = [] The data to be passed to the view.

Renders the view with the provided data.

This method prepares the view by bundling the data, creating the document, and loading the view content from the specified view file.

@param array $data The data to be passed to the view. @return View The current instance for method chaining.

State writes: $this->bundle.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
/**
     * Renders the view with the provided data.
     *
     * This method prepares the view by bundling the data, creating the document,
     * and loading the view content from the specified view file.
     *
     * @param array $data The data to be passed to the view.
     * @return View The current instance for method chaining.
     */
    public function view(array $data = []): View
    {
        $this->bundle = $data;

        $this->createBundleObjects();

        $this->createDocument($this->getViewContent());

        return $this;
    }

getViewContent

Source line 64

protected function getViewContent(): string

Loads the content of the view file and returns it as a string.

This method uses output buffering to capture the contents of the included view file and return it as a string.

@return string The content of the view file.

Return expressions (including local callbacks): ob_get_clean().

External effects include include.

Implementation, validation guards and interaction order
/**
     * Loads the content of the view file and returns it as a string.
     *
     * This method uses output buffering to capture the contents of the included
     * view file and return it as a string.
     *
     * @return string The content of the view file.
     */
    protected function getViewContent(): string
    {
        ob_start();
        include $this->viewDirectory . $this->viewName . '.awt.php';
        return ob_get_clean();
    }

render

Source line 78

public function render(): string

Renders the HTML document, processes templates, and dispatches a render-ready event. It applies template parsing for loops, conditions, assets, and variables before returning the final HTML string.

@throws Exception If any issues occur during rendering or template loading. @return string The final rendered HTML content as a string.

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

Failure handling catches Throwable.

Implementation, validation guards and interaction order
/**
     * Renders the HTML document, processes templates, and dispatches a render-ready event.
     * It applies template parsing for loops, conditions, assets, and variables before returning the final HTML string.
     *
     * @throws Exception If any issues occur during rendering or template loading.
     * @return string The final rendered HTML content as a string.
     */
    public function render(): string
    {
        $this->loadTemplate();
        $this->createBundleObjects();
        try {
            $parser = new BladeOne($this->viewDirectory, COMPILED, BladeOne::MODE_AUTO,
                csrfTokens: $this->csrfTokens ?? new \router\security\Csrf());
            // Also populate the property used by already-compiled @csrf templates.
            $parser->getCsrfToken();
            $parser->setFileExtension(".awt.php");

            $parser->addAssetDict(0, $this->localAssetPath);
            $parser->setPackageName($this->packageName);


            $parser->with($this->bundle);
            $compiled = $parser->run($this->viewName);

            $this->dom->loadHTML($compiled);

            $ready = new RenderReadyEvent();
            $ready->renderer = $this;
            $this->eventDispatcher->dispatch($ready);

            return $this->dom->saveHTML();

        } catch (Throwable $e) {
            if(DEBUG)
                $e->getTraceAsString();

            echo "An error has occurred while rendering: " . $e->getMessage();
        }
        return "";
    }

Related types

Clone this wiki locally