Code

Reference for the Code module: emit HTML elements and JavaScript function calls from nested PHP callbacks. Each element or call runs its callback with output buffering, capturing the attributes or arguments declared inside it, then writes the finished markup or code, with values escaped as they are emitted.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes

CodeLanguageHtml

Emit HTML elements from nested callbacks. An element function runs its callback with output buffering; any calls to Attribute inside the callback are gathered into the public static $attributes array, and any buffered output becomes the element's inner content. The array is saved and restored around each element so elements can nest. Element and attribute names, and attribute values, are escaped as they are written.

Attribute

CodeLanguageHtml::Attribute(string $name, string $value): void

Add an attribute to the element currently being emitted. Called from the callback passed to an element function.

NameTypeDescription
$namestringThe attribute name.
$valuestringThe attribute value.

Returns void

Div

CodeLanguageHtml::Div(callable $callback): void

Emit a div element, running the callback to gather its attributes and inner content.

NameTypeDescription
$callbackcallableDeclares attributes and writes the inner content of the div.

Returns void

Element

CodeLanguageHtml::Element(string $elementName, callable $callback): void

Emit a named element: run the callback with output buffering, gather its attributes and buffered inner content, then write the opening tag with attributes, the content and the closing tag. The attribute array is restored afterwards so elements can nest.

NameTypeDescription
$elementNamestringThe element tag name.
$callbackcallableDeclares attributes and writes the inner content of the element.

Returns void

JavaScript

CodeLanguageHtml::JavaScript(callable $callback): void

Emit a script element whose body is the buffered output of the callback, escaped for a JavaScript context inside HTML.

NameTypeDescription
$callbackcallableWrites the JavaScript that becomes the script body.

Returns void

CodeLanguageJavaScript

Emit JavaScript function calls from nested callbacks. Call runs an arguments callback with output buffering; any calls to the argument functions inside it are gathered into the public static $arguments array, then joined with commas and written as the call. The array is saved and restored around each call so calls can nest. Optionally the call's result is written into a new constant.

ArgumentCallback

CodeLanguageJavaScript::ArgumentCallback(callable $callback): void

Add an argument that is an arrow function, whose body is the buffered output of the callback. Called from the arguments callback passed to Call.

NameTypeDescription
$callbackcallableWrites the body of the arrow function.

Returns void

ArgumentConstant

CodeLanguageJavaScript::ArgumentConstant(mixed $value): void

Add an argument that is a constant value, JSON-encoded, or undefined when the value is null. Called from the arguments callback passed to Call.

NameTypeDescription
$valuemixedThe value to encode as the argument.

Returns void

ArgumentRaw

CodeLanguageJavaScript::ArgumentRaw(string $rawJavaScript): void

Add an argument that is written as raw JavaScript, unchanged. Called from the arguments callback passed to Call.

NameTypeDescription
$rawJavaScriptstringThe raw JavaScript for the argument.

Returns void

ArgumentReference

CodeLanguageJavaScript::ArgumentReference(string $name): void

Add an argument that is a reference to a named identifier. Called from the arguments callback passed to Call.

NameTypeDescription
$namestringThe identifier name to pass as the argument.

Returns void

Call

CodeLanguageJavaScript::Call(string|null $optionalTarget, string $functionName, callable $argumentsCallback): void

Emit a JavaScript function call. Run the arguments callback to gather its arguments, join them with commas, and write the call; when a target is given, assign the result to a new constant of that name.

NameTypeDescription
$optionalTargetstring|nullThe name of a constant to receive the return value, or null for no assignment.
$functionNamestringThe function to call.
$argumentsCallbackcallableDeclares the arguments to the call.

Returns void