PHP Luminova: XML and XHTML Markups Builder
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
- Class namespace:
Luminova\Components\Widget\Xhtml - Parent class: Luminova\Components\Widget\Document
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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$method | string | The name of the method being called, converted to lowercase to use as HTML tag name. |
$arguments | array | The 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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$tag | string | The HTML tag to be generated (e.g., 'div', 'span', 'p'). |
$content | string|null | Optional content to be placed inside the element (default: ''). |
$closeElement | bool | Whether to close the tag with content or self-close it (default: true). |
$attributes | array | Optional 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$elements | array | Array 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$tag | string | The HTML tag to generate (e.g., 'div', 'p', 'span'). |
$attributes | array<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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | The content to include within the comment. |
$hide | bool | Enable to hide scripts from browsers without support for scripts, so it doesn't show as plain text. |
$conditions | string|null | Optional 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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | The CSS content to be included within the style tag. |
$attributes | array<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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | The JavaScript content to be included within the script tag. |
$attributes | array<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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | The content to be included within the div. |
$attributes | array<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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$content | string | The content to be included within the div. |
$focusable | bool | Whether the element should be focusable or not. |
$attributes | array<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 = []
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$items | array<int,string<array<string,string>>|string | The 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'. |
$type | string | The type of list to generate ('ul' for unordered, 'ol' for ordered). Default is 'ul'. |
$attributes | array<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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$list | string | The HTML list items. |
$attributes | array | Optional 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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$list | string | The HTML list items. |
$attributes | array | Optional HTML attributes for the list. |
Return Value:
string - Return the generated unordered list.
See Also:
- https://www.w3schools.com/TAGS/tag_ul.asp
link
Generates a link element (a).
public static link(string $url, string|null $text = null, array $attributes = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$url | string | The URL for the link. |
$text | string|null | The visible text for the link or null to display url as text. |
$attributes | array | Optional 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 = []): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$src | string | The source URL for the image. |
$attributes | array | Optional HTML attributes for the image. |
Return Value:
string - Return the generated image element.
picture
Generates a picture element with a fallback img.