An open API service indexing awesome lists of open source software.

https://github.com/floofies/microvdom

A virtual Document Object Model for PHP and JavaScript
https://github.com/floofies/microvdom

classes html html-renderer minimalist php virtual-dom

Last synced: 6 months ago
JSON representation

A virtual Document Object Model for PHP and JavaScript

Awesome Lists containing this project

README

          

# microVDom

**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).

___

# Usage

Here is a small usage example:
```PHP
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:
```HTML




Hello World!

```

# Documentation

## `microDocument`

###### Class

```JavaScript
new microDocument( string $docType );
```

The primary controller/container class.

### Instantiating
Returns a new instance of the `microDocument` class. The new object has two children, the `DOCTYPE` declaration and ``; which then has two children as well, `` and ``.
```PHP
$document = new microDocument("html");
```

##### Constructor Parameters
- String **`docType`**

An arbitrary string used in the `` declaration.

### Member Variables
- String **`docType`**

An arbitrary string used in the `` declaration.

- Element **`documentElement`**

The direct child of the `microDocument`. Default is an `` element.

- Element **`body`**

The `` element of the `` element.

- Element **`head`**

The `` element of the `` element.

### Member Methods
#### `createAttribute`

###### Function
```PHP
microDocument->createAttribute( string $name , string $value );
```
Returns a clone of `object`.

##### Parameters
- String **`name`**

The name of the new attribute.

- String **`value`**

The value of the new attribute.

##### Example
```PHP
// 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:
```HTML


```

# WIP