This documentation describes unreleased changes on the main branch.

ViewData

Class.

use Saij\WpAppFramework\View\ViewData;

Data from PHP for the scripts of one framework instance: its views (of blocks, block supports and the document), its editor scripts and its shared modules read a value with this.data(key) or getData(key).

Inject it and set values before they are printed: on the front end the framework prints them with the view runtime in the footer, so values from hooks and from render() reach the views; in the editor it prints them with the editor runtime at the end of enqueue_block_editor_assets. Setting a value afterwards is an error. Every visitor can read the values.

#[Hook('wp')]
public function provideMap(ViewData $viewData): void
{
    $viewData->set('map', ['zoom' => 12]);
}

Methods

get()

get(string $key): mixed

The value set for the key.

ParameterTypeDefaultDescription
keystringrequiredThe key the value was set with.

Returns mixed

Throws OutOfBoundsException: If no value is set for the key.

has()

has(string $key): bool

Whether a value is set for the key.

ParameterTypeDefaultDescription
keystringrequiredThe key the value was set with.

Returns bool

set()

set(string $key, mixed $value): void

Sets a value; a later call with the same key replaces it.

ParameterTypeDefaultDescription
keystringrequiredLetters, digits, "-" and "_", starting with a letter.
valuemixedrequirednull, a scalar or an array of these (no objects, no NAN or INF).

Throws InvalidArgumentException: If the key or the value is invalid.

Throws LogicException: If the values are already printed and WP_DEBUG is on.