Luminova Framework

PHP Luminova: XHTML Document Builder

Last updated: 2026-11-28 20:57:12

The XML and XHTML Markup Builder is a flexible tool for generating well-structured XML and XHTML elements dynamically within your application.

When generating dynamic XML or HTML for the frontend, the Xhtml (Extensible Hypertext Markup Language) builder class in Luminova simplifies the process with static methods to create markup elements. It enables you to programmatically build tags, attributes, and content with clean, efficient syntax, It supports paired and self-closing tags and lowercases tag names by default. Supply well-formed nested markup and escape any untrusted values passed as element content.

For detailed instructions on generating elements, refer to the XHTML Examples Documentation to learn how to use the class for creating various HTML elements.


Class Definition


Properties Configuration

See the Base Document Builder Helper Class for more information relating to properties configuration.


template

The document element style to use (e.g, Xhtml::HTML5, Xhtml::BOOTSTRAP5).

Xhtml::$template = Xhtml::HTML5;

encoding

The encoding to use for escaping document element, contents and attributes.

Xhtml::$encoding = 'UTF-8';

xhtmlStrictTagNames

Determines whether element tag names should be automatically converted to lowercase (in strict XHTML mode) or preserved as they were passed, which can be useful for applications using custom template elements or case-sensitive tags (such as when working with XML-like data structures).

Usage Examples:

Xhtml::$xhtmlStrictTagNames = true;

echo Xhtml::element('MyCustomTag', 'Content'); 
// Generates: <mycustomtag>Content</mycustomtag>
Xhtml::$xhtmlStrictTagNames = false;

echo Xhtml::element('MyCustomTag', 'Content'); 
// Generates: <MyCustomTag>Content</MyCustomTag>

Methods

__callStatic

Dynamically generates an HTML element using static method calls.

This method allows calling methods that are not explicitly defined in the class.The arguments are passed positionally to element(). Tag names follow $xhtmlStrictTagNames. Pass true as the second argument to render a paired tag with content.

public static function __callStatic(string $method, array $arguments): string

Parameters:

ParameterTypeDescription
$methodstringThe name of the method being called, used as the HTML tag name.
$argumentsarray{0?:string\|null,1?:bool,2?:array<string,mixed>}The positional arguments passed to the method.
The arguments are unpacked as follows:
- string $content The string content to be passed to the element (default: '').
- bool $closeElement Pass true for a paired tag. The default false emits a self-closing tag.
- array<string,string> $attributes Additional HTML attributes for the element.

Return:

string - Returns the generated HTML element as a string.

Example usage:

echo Xhtml::div('Content', true, ['class' => 'my-class']);
echo Xhtml::p('Paragraph content', true, ['id' => 'my-paragraph']);

element

Generates an HTML element with optional attributes and content.

This method creates a specified HTML tag, with the option to includeattributes and content. The tag can either be a block element (with opening and closing tags) or a self-closing tag.

public static function element(string $tag, ?string $content = null, bool $closeElement = true, array $attributes = []): string

Parameters:

ParameterTypeDescription
$tagstringThe HTML tag to be generated (e.g., 'div', 'span', 'p').
$contentstring|nullOptional content to be placed inside the element (default: '').
$closeElementboolWhether to close the tag with content or self-close it (default: true).
$attributesarray<array-key, mixed>Optional associative array of attributes to be added
to the HTML tag (e.g., ['class' => 'my-class', 'id' => 'my-id']).

Return:

string - Returns the generated HTML element as a string.

Usage Examples:

Generate a div with content.

echo Xhtml::element('div', 'Hello, World!', true, ['class' => 'greeting']);

Output:

<div class="greeting">Hello, World!</div>

Generate a self-closing image tag.

echo Xhtml::element('img', null, false, ['src' => 'image.jpg', 'alt' => 'My Image']);

Output:

<img src="image.jpg" alt="My Image" />

elements

Generates multiple HTML elements based on an array of element specifications.

Each element in the array should be an associative array containing the following keys:

  • tag (string): The HTML tag to generate (e.g., 'div', 'p', 'span').
  • content (string): The content to be placed inside the HTML tag (default: empty string).
  • closeElement (bool): Whether to close the tag as a block element (default: true if content is provided).
  • attributes (array): Optional associative array of HTML attributes for the tag (default: empty array).
public static function elements(array $elements): string

Parameters:

ParameterTypeDescription
$elementsarray<int,array{tag:string,content?:string,closeElement?:bool,attributes?:array<string,mixed>}>Array of elements where each element is defined with keys ('tag', 'content', 'closeElement', and 'attributes').

Return:

string - Returns the generated HTML elements as a string.

Usage Examples:

$elements = [
    ['tag' => 'div', 'content' => 'Hello, World!', 'closeElement' => true, 'attributes' => ['class' => 'greeting']],
    ['tag' => 'p', 'content' => 'This is a paragraph.', 'attributes' => ['style' => 'color: blue;']],
    ['tag' => 'span', 'content' => 'Highlighted text', 'closeElement' => false, 'attributes' => ['class' => 'highlight']]
];

echo Xhtml::elements($elements);

Output:

<div class="greeting">Hello, World!</div>
<p style="color: blue;">This is a paragraph.</p>
<span class="highlight">Highlighted text</span>

tags

Generates multiple self-closing HTML elements of a specific tag, based on an array of element attributes.

Each element attribute should be an associative array containing theattributes to apply to the generated element.

public static function tags(string $tag, array $attributes): string

Parameters:

ParameterTypeDescription
$tagstringThe HTML tag to generate (e.g., 'div', 'p', 'span').
$attributesarray<int, array<string, mixed>>Array of element attributes,
where each entry is an associative array of attributes for a single element.

Return:

string - Returns the generated HTML elements as a string.

Usage Examples:

$attributes = [
    ['class' => 'item', 'data-id' => '1'],
    ['class' => 'item', 'data-id' => '2'],
    ['class' => 'item', 'data-id' => '3']
];

echo Xhtml::tags('hr', $attributes);

Output:

<hr class="item" data-id="1" />
<hr class="item" data-id="2" />
<hr class="item" data-id="3" />

comment

Generate an inline comment block or conditional comment with the given content.

This method creates a conditional comment for Internet Explorer based on the provided conditions. If no conditions are specified, it generates a standard HTML comment. Optionally, the comment can be marked as hidden by appending // at the end.

public static function comment(string $content, bool $hide = false, ?string $conditions = null): string

Parameters:

ParameterTypeDescription
$contentstringThe content to include within the comment.
$hideboolEnable to hide scripts from browsers without support for scripts, so it doesn't show as plain text.
$conditionsstring|nullOptional conditions for generating a conditional comment (default: null).

Return:

string - Return the generated HTML comment string.


css

Generate an inline style element with the given CSS content.

This method escapes the provided CSS content to prevent XSS and returns thegenerated HTML for the style element.

public static function css(string $content, array $attributes = []): string

Parameters:

ParameterTypeDescription
$contentstringThe CSS content to be included within the style tag.
$attributesarray<string, string>Additional HTML attributes for the style element (e.g., class, id).

Return:

string - Return the generated HTML style element as a string.


js

Generate an inline script element with the given JavaScript content.

This method escapes the provided JavaScript content to prevent XSS and returns thegenerated HTML for the script element.

public static function js(string $content, array $attributes = []): string

Parameters:

ParameterTypeDescription
$contentstringThe JavaScript content to be included within the script tag.
$attributesarray<string, string>Additional HTML attributes for the script element (e.g., async, defer).

Return:

string - Return the generated HTML script element as a string.


hidden

Generate a hidden div element with the given content.

This method sets the display style to 'none' to hide the element from view.

public static function hidden(string $content, array $attributes = []): string

Parameters:

ParameterTypeDescription
$contentstringThe content to be included within the div.
$attributesarray<string, string>Additional HTML attributes for the div (e.g., class, id).

Return:

string - Return the generated HTML div element as a string.


invisible

Generate an invisible div element with the given content.

This method either hides the element with absolute positioning (focusable)or sets its visibility to hidden, based on the focusable parameter.

public static function invisible(string $content, bool $focusable = true, array $attributes = []): string

Parameters:

ParameterTypeDescription
$contentstringThe content to be included within the div.
$focusableboolWhether the element should be focusable or not.
$attributesarray<string, string>Additional HTML attributes for the div (e.g., class, id).

Return:

string - Return the generated HTML div element as a string.


list

Generate an HTML list (unordered or ordered) from a set of items.

This method supports both an array of items and a single string.If the input is an array, each item can include its content and attributes.

public static function list(
    array|string $items,
    string $type = 'ul',
    array $attributes = []
): string

Parameters:

ParameterTypeDescription
$itemsarray<int, string<array<string, string>>|stringThe items to be included in the list; can be a single string or an array of items.
Each item can be a string or an associative array with 'content' and 'attributes'.
$typestringThe type of list to generate ('ul' for unordered, 'ol' for ordered). Default is 'ul'.
$attributesarray<string, string>Additional HTML attributes for the list element (e.g., class, id).

Return:

string - Return the generated HTML list as a string.


ol

Generates an ordered list (ol) with items.

public static function ol(string $list, array $attributes = []): string

Parameters:

ParameterTypeDescription
$liststringThe HTML list items.
$attributesarray<array-key, mixed>Optional HTML attributes for the list.

Return:

string - Return the generated ordered list.

See Also:

  • https://www.w3schools.com/TAGS/tag_ol.asp

ul

Generates an unordered list (ul) with items.

public static function ul(string $list, array $attributes = []): string

Parameters:

ParameterTypeDescription
$liststringThe HTML list items.
$attributesarray<array-key, mixed>Optional HTML attributes for the list.

Return:

string - Return the generated unordered list.

See Also:

  • https://www.w3schools.com/TAGS/tag_ul.asp

href

Generates an anchor element. Escape untrusted link text before passing it as content.

public static function href(string $url, ?string $text = null, array $attributes = []): string

Parameters:

ParameterTypeDescription
$urlstringThe URL for the link.
$textstring|nullThe visible text for the link or null to display url as text.
$attributesarray<array-key, mixed>Optional HTML attributes for the link.

Return:

string - Return the generated link.


image

Generates an image element (img).

public static function image(string $src, array $attributes = []): string

Parameters:

ParameterTypeDescription
$srcstringThe source URL for the image.
$attributesarray<array-key, mixed>Optional HTML attributes for the image.

Return:

string - Return the generated image element.


picture

Generates a picture element with a fallback img.

public static function picture(
    string $src,
    array|string $source,
    array $attributes = ['image' => [], 'picture' => []]
): string

Parameters:

ParameterTypeDescription
$srcstringThe source URL for the image.
$sourcearray<array-key, mixed>&#124;stringThe source(s) for the picture.
$attributesarray<string, array<array-key, mixed>>Optional attributes for the img and picture tags.
- 'image' => (array) Attributes for the fallback &lt;img&gt; tag (e.g., 'alt', 'style').
- 'picture' => (array) Attributes for the &lt;picture&gt; tag.

Return:

string - Return the generated picture element.


figure

Generates a figure element containing an image with an optional caption.

public static function figure(
    string $src,
    string $caption,
    array $attributes = ['image' => [], 'figure' => []]
): string

Parameters:

ParameterTypeDescription
$srcstringThe URL of the image.
$captionstringThe caption text for the image.
$attributesarray<string, array<array-key, mixed>>Optional attributes for the img and figure tags.
- 'image' => (array) Attributes for the &lt;img&gt; tag (e.g., 'alt', 'style').
- 'figure' => (array) Attributes for the &lt;figure&gt; tag.

Return:

string - Return the generated figure element.


source

Generates an HTML <source> element for media content, such as <video> or <audio> tags.

public static function source(string $src, string $type = 'audio/mpeg', array $attributes = []): string

Parameters:

ParameterTypeDescription
$srcstringThe URL of the media file.
$typestringThe MIME type of the media file (e.g., 'video/mp4', 'audio/mpeg').
$attributesarray<array-key, mixed>Optional attributes for the source element.

Return:

string - Return the generated HTML &lt;source&gt; element as a string.

See Also:

  • https://www.w3schools.com/TAGS/tag_source.asp

track

Generates an HTML <track> element for media content, such as <video> or <audio> tags.

public static function track(string $src, string $kind = 'subtitles', array $attributes = []): string

Parameters:

ParameterTypeDescription
$srcstringThe URL of the media file.
$kindstringSpecifies the kind of text track (e.g., 'captions', 'chapters', descriptions, metadata or subtitles).
$attributesarray<array-key, mixed>Optional attributes for the source element.

Return:

string - Return the generated HTML &lt;track&gt; element as a string.

See Also:

  • https://www.w3schools.com/TAGS/tag_track.asp

param

Generates an HTML <param> element for media content, such as <object> tags.

public static function param(string $name, string $value): string

Parameters:

ParameterTypeDescription
$namestringThe name of a parameter (e.g, autoplay).
$valuestring

Return:

string - Return the generated HTML &lt;param&gt; element as a string.

See Also:

  • https://www.w3schools.com/TAGS/tag_param.asp

video

Generates a video element.

public static function video(
    array|string $source,
    array|string $tracks = [],
    array $attributes = [],
    string $placeholder = 'Your browser does not support the video tag.'
): string

Parameters:

ParameterTypeDescription
$sourcearray<array-key, mixed>&#124;stringThe source(s) for the video.
$tracksarray<array-key, mixed>&#124;stringThe track(s) for the video.
$attributesarray<array-key, mixed>Optional attributes for the video element.
$placeholderstringFallback text if the video is unsupported.

Return:

string - Return the generated video element.

See Also:

  • https://www.w3schools.com/TAGS/tag_video.asp

audio

Generates an audio element.

public static function audio(
    array|string $source,
    array|string $tracks = [],
    array $attributes = [],
    string $placeholder = 'Your browser does not support the audio tag.'
): string

Parameters:

ParameterTypeDescription
$sourcearray<array-key, mixed>&#124;stringThe source(s) for the audio.
$tracksarray<array-key, mixed>&#124;stringThe track(s) for the audio.
$attributesarray<array-key, mixed>Optional attributes for the audio element.
$placeholderstringFallback text if the audio is unsupported.

Return:

string - Return the generated audio element.

See Also:

  • https://www.w3schools.com/TAGS/tag_audio.asp

iframe

Generates an iframe element.

public static function iframe(
    string $src,
    string $placeholder = 'Your browser does not support the iframe tag.',
    array $attributes = []
): string

Parameters:

ParameterTypeDescription
$srcstringThe source URL for the iframe content.
$placeholderstringThe fallback text or content inside the iframe element.
$attributesarray<array-key, mixed>Optional HTML attributes for the iframe.

Return:

string - Return the generated iframe element.


map

Generates an HTML image map with the specified areas and attributes.

This creates an <img> element with a usemap attribute, along with the associated <map> and <area> tags.

public static function map(
    string $src,
    string $name,
    string|array $areas,
    array $attributes = ['image' => [], 'map' => []]
): string

Parameters:

ParameterTypeDescription
$srcstringThe source URL of the image to be used in the map.
$namestringThe name of the image map (used in the "usemap" attribute).
$areasstring\|array<int,array<string,mixed>>Prebuilt area markup or one attribute array per area.
$attributesarray{image:array<string,mixed>,map?:array<string,mixed>}Attributes for the img and map elements. If passing a custom array, include the image key.
- 'image' => array for <img> element attributes
- 'map' => array for <map> element attributes

Return:

string - Return the complete HTML string for the image and the associated map element.


document

Generates a full basic HTML document structure.

For $headers argument, each element in the array should be an associative array containing the following keys:

  • tag (string): The HTML tag to generate (e.g., 'meta', 'link', 'script').
  • content (string): Optional content inside the tag (e.g., for script).
  • closeElement (bool): Whether to close the tag as a block element (default: true if content is provided).
  • attributes (array): Optional associative array of HTML attributes for the tag.
public static function document(
    string $content,
    string $title,
    string $doctype = 'html5',
    string|array|null $headers = null,
    array $attributes = ['html' => [], 'body' => []]
): string

Parameters:

ParameterTypeDescription
$contentstringThe HTML content of the page.
$titlestringThe title of the page.
$doctypestringThe HTML document type declaration (default: 'html5').
$headersstring&#124;array<array-key, mixed>&#124;nullOptional elements for the document's <head> (e.g., <meta>, <link>, <script>).
$attributesarray<string, array<array-key, mixed>>Optional attributes for the <html> and <body> tags.
- 'html' => attributes for the <html> tag.
- 'body' => attributes for the <body> tag.

Return:

string - Return the generated HTML document.


xml

Generates a full basic XML document structure with a flexible doctype and additional options.

public static function xml(
    string $content,
    string $version = '1.0',
    string $encoding = 'UTF-8',
    bool $standalone = true,
    string $doctype = 'xhtml11',
    array $attributes = []
): string

Parameters:

ParameterTypeDescription
$contentstringThe XML content of the document.
$versionstringThe XML version (default: '1.0').
$encodingstringThe character encoding of the document (default: 'UTF-8').
$standaloneboolWhether the XML document is standalone (default: true).
$doctypestringThe XML document type declaration (default: 'xhtml11').
$attributesarray<array-key, mixed>Optional attributes for the root element.

Return:

string - Return the generated XML document.


svg

Generates a full basic XML document structure.

public static function svg(string $content, string $doctype = 'svg10', array $attributes = []): string

Parameters:

ParameterTypeDescription
$contentstringThe SVG content of the document.
$doctypestringThe SVG document type (default:svg10).
$attributesarray<array-key, mixed>Optional attributes for the svg tag.

Return:

string - Return the generated HTML document.


table

Generates an HTML table with headers, body, and footers.

public static function table(
    string|array $tbody,
    string|array|null $thead = null,
    string|array|null $tfoot = null,
    string|array|null $colgroup = null,
    array $attributes = [],
    ?string $caption = null
): string

Parameters:

ParameterTypeDescription
$tbodystring&#124;array<array-key, mixed>The table body (string or array).
$theadstring&#124;array<array-key, mixed>&#124;nullThe table headers (string or array).
$tfootstring&#124;array<array-key, mixed>&#124;nullThe table footers (string or array).
$colgroupstring&#124;array<array-key, mixed>&#124;nullAttributes for table column group (string or array).
$attributesarray<array-key, mixed>Optional HTML attributes for the table.
$captionstring|nullOptional caption for the table (default: null).

Return:

string - Return the generated HTML table.


tcol

Generates a table column group with type.

public static function tcol(array $columns, array $attribute = []): string

Parameters:

ParameterTypeDescription
$columnsarray<int, array<string, mixed>>An array of table columns attributes for each row.
$attributearray<array-key, mixed>

Return:

string - Return the generated colgroup and columns element.


tcell

Generates a table cell with type.

public static function tcell(string $parent, string $type, array $rows, array $attribute = []): string

Parameters:

ParameterTypeDescription
$parentstringTable cell parent element type (e.g, thead, tbody, tfoot).
$typestringTable cell children type (e.g, td, th).
$rowsarray<array-key, mixed>The rows of the table body. Each row can be:
- An array of strings (e.g., ['foo', 'bar'])
- An array of associative arrays (e.g., [['content' => 'Foo', 'attributes' => [...]], ...])
$attributearray<array-key, mixed>

Return:

string - Return the generated table cell element.


tbody

Generates a table body (tbody) with rows.

public static function tbody(array $rows, string $type = 'td', array $attributes = []): string

Parameters:

ParameterTypeDescription
$rowsarray<array-key, mixed>The rows of the table body. Each row can be:
- An array of strings (e.g., ['foo', 'bar'])
- An array of associative arrays (e.g., [['value' => 'Foo', 'attributes' => [...]], ...]).
$typestringThe child element tag name of the table tbody (default: td).
$attributesarray<array-key, mixed>Optional HTML attributes for the tbody.

Return:

string - Return the generated tbody element.


thead

Generates a table head (thead) with rows.

public static function thead(array $rows, string $type = 'th', array $attributes = []): string

Parameters:

ParameterTypeDescription
$rowsarray<array-key, mixed>The rows of the table head.
$typestringThe child element tag name of the table thead (default: th).
$attributesarray<array-key, mixed>Optional HTML attributes for the thead.

Return:

string - Return the generated thead element.


tfoot

Generates a table footer (tfoot) with rows.

public static function tfoot(array $rows, string $type = 'td', array $attributes = []): string

Parameters:

ParameterTypeDescription
$rowsarray<array-key, mixed>The rows of the table footer.
$typestringThe child element tag name of the table tfoot (default: td).
$attributesarray<array-key, mixed>Optional HTML attributes for the tfoot.

Return:

string - Return the generated tfoot element.