Baigudin Software avatar
Baigudin Software logo
home
ru region
en region

PHP DOM Builder API 2.0

PHP DOM Builder is unveiled in second revision

In 2014 year PHP DOM Builder library was unveiled at the first time within the framework of Baigudin Software project. Two years have been left after that moment and today we are glad to offer the second generation that is PHP DOM Builder Revision 2.

Beginning of work

So, we have included the library and set it. Now we can create a basic HTML document.

<?php
use DomBuilder\Element as Element
// Create document 
$document Element::create(); 
// Add main tags to document 
$node $document->insert('html'); 
$node->insert('head'); 
$node->insert('body'); 
// Output document
echo Element::getDocument($document);
?>

The result of this example is here:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head></head>
  <body></body>
</html>

Every HTML document can be represented as a tree list of elements . In our example, we have created a root element of document and linked some elements to it. All methods for creating elements, such as create, insert, after,and before, create a new element and return a reference to it. That is why we could rewrite our example like here:

<?php
use DomBuilder\Element as Element
// Create tree list 
$root Element::create('html')
  ->
insert('head')   
  ->
after('body')   
  ->
root(); 
// Output document 
echo Element::getDocument($root);
?>

If we want to get a correct document, we need to give getDocument method a root element of document. It is possible by using root method, which travels all DOM tree and returns a reference to a root element.

To the previous article

To the articles list

To the next article