PHP
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src Consistent null. Nov 23, 2016
test
.gitignore
.scrutinizer.yml update php version Jul 17, 2016
CONTRIBUTING.md Add anchors Mar 2, 2016
LICENSE Update LICENSE Feb 29, 2016
README.md Rename icon to logo Feb 7, 2017
circle.yml
composer.json
composer.lock
example.gif
logo.png
phpunit.xml Rename test helper class Feb 3, 2016

README.md

The modern DOM API for PHP 7 projects

The modern DOM API for PHP 7 projects.

Built on top of PHP's native DOMDocument, this project provides access to modern DOM APIs, as you would expect working with client-side code in the browser.

Performing DOM manipulation in your server-side code enhances the way dynamic pages can be built. Utilising a standardised object-oriented interface means the page can be ready-processed, benefitting browsers, webservers and content delivery networks.


Build status Code quality Code coverage Current version

Example usage: Hello, you!

Consider a page with a form, with an input element to enter your name. When the form is submitted, the page should greet you by your name.

This is a simple example of how source HTML files can be treated as templates. This can easily be applied to more advanced template pages to provide dynamic content, without requiring non-standard techniques such as {{curly braces}} for placeholders, or echo '<div class='easy-mistake'>' . $content['opa'] . '</div>' horrible HTML construction from within PHP.

Source HTML (name.html).

<!doctype html>
<h1>
    Hello, <span id="your-name">you</span> !
</h1>

<form>
    <input name="name" placeholder="Your name, please" required />
    <button>Submit</button>
</form>

PHP used to inject your name (index.php).

<?php
require "vendor/autoload.php";

$html = file_get_contents("name.html");
$document = new \Gt\Dom\HTMLDocument($html);

if(isset($_GET["name"])) {
    $document->getElementById("your-name")->textContent = $_GET["name"];
}

echo $document->saveHTML();

Screencast of above example:

Using DOM in PHP

Features at a glance.

Non-standard features.

The following features are currently in development and are planned to be released into version 2.0.

  • Server-side WebComponent support:
    • <calendar> <date>1988-04-05</date> <caption>My birthday</caption> </calendar>.
    • <button is="shopping-cart">Add to cart</button>.
  • Templating to ease dynamic content:
    • <template id="list-item"> <li> Add this many times </li> </template>.
    • <tr data-template="table-row"> <td> Add this many times. </td> </tr>.
  • Reference an element's children using CSS query selectors via ArrayAccess:
    • $container["h1"][0]->textContent = "Hello, World!";.
    • $document["body>header>nav li a"][0]->href = "/nowhere.html";.
    • foreach($myElement["input[type='checkbox']"] as $checkbox) {.