Luminova Framework

PHP Luminova: XML and XHTML Markup Builder

Last updated: 2024-11-04 20:09:39

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, ensuring compliance with XML and XHTML standards. This includes enforcing case sensitivity, proper nesting, and closing all tags (even self-closing ones). Tag names are automatically converted to lowercase, making the class ideal for applications that require strict document formatting, such as web pages, feeds, or data exchanges.

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 method name is converted to lowercase and the arguments are unpacked into relevant parameters for the element method.

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

Parameters:

ParameterTypeDescription
$methodstringThe name of the method being called, converted to lowercase to use as HTML tag name.
$argumentsarrayThe 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 Whether the element should be self-closing (default: false).
- array<string,string> $attributes Additional HTML attributes for the element.

Return Value:

string - Returns the generated HTML element as a string.

Example usage:

echo Xhtml::div('Content', ['class' => 'my-class']);
echo Xhtml::p('Paragraph content', ['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 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).
$attributesarrayOptional associative array of attributes to be added
to the HTML tag (e.g., ['class' => 'my-class', 'id' => 'my-id']).

Return Value:

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 elements(array $elements): string

Parameters:

ParameterTypeDescription
$elementsarrayArray of elements where each element is defined with keys ('tag', 'content', 'closeElement', and 'attributes').

Return Value:

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 non-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 tags(string $tag, array<int,array<string,mixed>> $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 Value:

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 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 Value:

string - Return he 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 css(string $content, array<string,string> $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 Value:

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 js(string $content, array<string,string> $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 Value:

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 hidden(string $content, array<string,string> $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 Value:

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 invisible(string $content, bool $focusable = true, array<string,string> $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 Value:

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 list(
    array<int,string|array<string,string>>|string $items, 
    string $type = 'ul', 
    array<string,string> $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 Value:

string - Return the generated HTML list as a string.


ol

Generates an ordered list (ol) with items.

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

Parameters:

ParameterTypeDescription
$liststringThe HTML list items.
$attributesarrayOptional HTML attributes for the list.

Return Value:

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 ul(string $list, array $attributes = []): string

Parameters:

ParameterTypeDescription
$liststringThe HTML list items.
$attributesarrayOptional HTML attributes for the list.

Return Value:

string - Return the generated unordered list.

See Also:

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

Generates a link element (a).

public static link(string $url, string|null $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.
$attributesarrayOptional HTML attributes for the link.

Return Value:

string - Return the generated link.


image

Generates an image element (img).

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

Parameters:

ParameterTypeDescription
$srcstringThe source URL for the image.
$attributesarrayOptional HTML attributes for the image.

Return Value:

string - Return the generated image element.


picture

Generates a picture element with a fallback img.

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

Parameters:

ParameterTypeDescription
$srcstringThe source URL for the image.
$sourcearray|stringThe source(s) for the picture.
$attributesarrayOptional 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 Value:

string - Return the generated picture element.


figure

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

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

Parameters:

ParameterTypeDescription
$srcstringThe URL of the image.
$captionstringThe caption text for the image.
$attributesarrayOptional 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 Value:

string - Return the generated figure element.


source

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

public static 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').
$attributesarrayOptional attributes for the source element.

Return Value:

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 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).
$attributesarrayOptional attributes for the source element.

Return Value:

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 param(string $name, string $value): string

Parameters:

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

Return Value:

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 video(
    array|string $source, 
    array|string $tracks = [], 
    array $attributes = [], 
    string $placeholder = 'Your browser does not support the video tag.'
): string

Parameters:

ParameterTypeDescription
$sourcearray|stringThe source(s) for the video.
$tracksarray|stringThe track(s) for the video.
$attributesarrayOptional attributes for the video element.
$placeholderstringFallback text if the video is unsupported.

Return Value:

string - Return the generated video element.

See Also:

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

audio

Generates an audio element.

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

Parameters:

ParameterTypeDescription
$sourcearray|stringThe source(s) for the audio.
$tracksarray|stringThe track(s) for the audio.
$attributesarrayOptional attributes for the audio element.
$placeholderstringFallback text if the audio is unsupported.

Return Value:

string - Return the generated audio element.

See Also:

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

iframe

Generates an iframe element.

public static 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.
$attributesarrayOptional HTML attributes for the iframe.

Return Value:

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 map(
    string $src, 
    string $name, 
    array $areas, 
    array<string,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|arrayAn array or string defining the areas within the map.
$attributesarray<string,array>Optional attributes for the img and map elements.
- 'image' => array for <img> element attributes
- 'map' => array for <map> element attributes

Return Value:

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 document(
    string $content, 
    string $title, 
    string $doctype = 'html5', 
    string|array|null $headers = null, 
    array<string,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|array|nullOptional elements for the document's <head> (e.g., <meta>, <link>, <script>).
$attributesarrayOptional attributes for the <html> and <body> tags.
- 'html' => attributes for the <html> tag.
- 'body' => attributes for the <body> tag.

Return Value:

string - Return the generated HTML document.


xml

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

public static 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').
$attributesarrayOptional attributes for the root element.

Return Value:

string - Return the generated XML document.


svg

Generates a full basic XML document structure.

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

Parameters:

ParameterTypeDescription
$contentstringThe SVG content of the document.
$doctypestringThe SVG document type (default:svg10).
$attributesarrayOptional attributes for the svg tag.

Return Value:

string - Return the generated HTML document.


table

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

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

Parameters:

ParameterTypeDescription
$tbodystring|arrayThe table body (string or array).
$theadstring|array|nullThe table headers (string or array).
$tfootstring|array|nullThe table footers (string or array).
$colgroupstring|array|nullAttributes for table column group (string or array).
$attributesarrayOptional HTML attributes for the table.
$captionstring|nullOptional caption for the table (default: null).

Return Value:

string - Return the generated HTML table.


tcol

Generates a table column group with type.

public static tcol(array<int,array<string,mixed>> $columns, array $attribute = []): string

Parameters:

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

Return Value:

string - Return the generated colgroup and columns element.


tcell

Generates a table cell with type.

public static 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).
$rowsarrayThe 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

Return Value:

string - Return the generated table cell element.


tbody

Generates a table body (tbody) with rows.

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

Parameters:

ParameterTypeDescription
$rowsarrayThe 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).
$attributesarrayOptional HTML attributes for the tbody.

Return Value:

string - Return the generated tbody element.


thead

Generates a table head (thead) with rows.

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

Parameters:

ParameterTypeDescription
$rowsarrayThe rows of the table head.
$typestringThe child element tag name of the table thead (default: th).
$attributesarrayOptional HTML attributes for the thead.

Return Value:

string - Return the generated thead element.


tfoot

Generates a table footer (tfoot) with rows.

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

Parameters:

ParameterTypeDescription
$rowsarrayThe rows of the table footer.
$typestringThe child element tag name of the table tfoot (default: td).
$attributesarrayOptional HTML attributes for the tfoot.

Return Value:

string - Return the generated tfoot element.