microVDom is a tiny (under 300 lines) PHP Virtual DOM solution for minimalist creation and rendering of HTML.
Only the bare minimum of DOM methods are included to quickly write a presentation layer, completely eschewing parsing and query methods. Only a small subset of command methods are supported.
Due to the lack of mutating command methods, the microDocument
is effectively WORM (Write Once, Read Many).
Here is a small usage example:
<?php
// Include the microVDom file:
include("microVDom.php");
// Create a new empty microDocument:
$document = new microDocument("html");
// Create a new div with a class attribute:
$myDiv = $document->createElement("div", ["class" => "myClass"]);
// Add a text node to the div:
$myDiv->appendChild("Hello World!");
// Append the document body with the div:
$document->body->appendChild($myDiv);
// Render the HTML:
$myHTML = $document->render();
?>
When the code runs, $myHTML
will contain the following HTML string:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="myClass">Hello World!</div>
</body>
</html>
new microDocument( string $docType );
The primary controller/container class.
Returns a new instance of the microDocument
class. The new object has two children, the DOCTYPE
declaration and <html>
; which then has two children as well, <head>
and <body>
.
$document = new microDocument("html");
-
String
docType
An arbitrary string used in the
<!DOCTYPE ...>
declaration.
-
String
docType
An arbitrary string used in the
<!DOCTYPE ...>
declaration. -
Element
documentElement
The direct child of the
microDocument
. Default is an<html>
element. -
Element
body
The
<body>
element of the<html>
element. -
Element
head
The
<head>
element of the<html>
element.
microDocument->createAttribute( string $name , string $value );
Returns a clone of object
.
-
String
name
The name of the new attribute.
-
String
value
The value of the new attribute.
// Instantiate a new Attr Object
$myAttr = $document->createAttribute("myAttr", "myValue");
// Instantiate a new Element Object
$myDiv = $document->createElement("div");
// Set the new attribute on the Element Object
$myDiv->setAttribute($myAttr);
// Render the HTML
$myHTML = $myDiv->render();
$myHTML
now contains an HTML string:
<div myAttr="myValue"></div>