Object-oriented HTML generation
Thursday, 30 December 2010
Article Index
Object-oriented HTML generation
Object-oriented approach
PHPQuery

 

New DOMElements

Of course it would look better with the button wrapped in a new class but this is where things start to get difficult. The logical way to do things is to extend DOMElement. For example:

class Button extends DOMElement
{
function __construct(){
parent::__construct('input');
$this->setAttribute('type','button');
}
}

but this fails for a number of reasons but mainly because a DOMElement created using the constructor is read only. This means that any attempt to set attributes after creation fails. 

Banner

What this means is that you have to create any new DOMElements in association with a DOMDocument and this makes building a clean object-oriented hierarchy difficult. The only reasonable approach is to create a new class that extends DOMDocument and then write methods which create all of the elements you need. For example:

class HTML extends DOMDocument
{
function __construct(){
parent::__construct(
'1.0','iso-8859-1' );
$this->formatOutput = true;
}

By overriding the constructor we have a class better suited to HTML.

Next we need a new create method:

public  function createButton(){
$temp= $this->createElement('input');
$temp->setAttribute('type','button');
return $temp;
}

We don't have to, but we might as well, override the Save method to ensure the encoding is correct:

public  function saveHTML(){
return  html_entity_decode(
parent::saveHTML());
}

Now we can use the new class to create the standard button example:

$HTML=new HTML();
$elem=$HTML->createButton();
$elem->setAttribute(
'value','My Button');
$elem->setAttribute(
'style','width:125px');
$HTML->appendChild($elem);
echo $HTML->saveHTML();

You can add other create methods and other helper methods as required. It isn't difficult but it really shouldn't be necessary. There should be a good off-the-shelf solution and perhaps there is.

PHPQuery

If you have heard of or used JQuery you probably know that it is a Javascript library that allows you to manipulate the DOM on the client side. PHPQuery is a PHP version of JQuery intended for use on the server side. In most cases PHPQuery and JQuery are used to analyse an existing DOM, but there are big advantages to using them for a slightly less obvious task - generating HTML via the DOM.

Using PHPQuery has an even bigger advantage if you are already using JQuery on the client side - after all why learn two different approaches.

You can download PHPQuery from:

http://code.google.com/p/phpquery/

where you will also find documentation and a community site.

Once you have the PHP library installed you can use it to generate the button example very simply:

$doc = phpQuery::newDocument();
$button=pq('<input>')
->attr('type','button')
->attr('style','width:150px')
->attr('value','My Button');
$doc->append($button);
echo $doc;

The steps in the program are very simple. First we open a new document, then create a PHPQuery button object, complete with attribute settings, append the object to the DOM, and finally generate the HTML.

It really is this easy and notice the way that the method calls to set the attributes can be chained together - just like JQuery.  If you already know JQuery than the only extra information you really need is the fact that the pq function behaves like the Javascript JQuery or $ function.

Of course there is more to PHPQuery than generating simple buttons and you can use it to process a DOM in complex ways in an efficient style that characterises JQuery. It even supports server side Ajax using a similar set of method calls!

PHPQuery deserves to be better known.

The future

Generating HTML is PHP's main role in life and as such it is surprising that it does it so badly. Quoting HTML tags is an easy way to get beginners started, but the horrible mix of HTML and PHP that results when you try to generate a real web page is far from good. PHP needs an official, i.e. built-in to the language system, object-oriented way of generating HTML. Until such time either create your own HTML objects via the PHP DOM or be adventurous and try PHPQuery.

 

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook, Digg or you can subscribe to our weekly newsletter.

Banner


Developing with PHP and Eclipse (Indigo)

Find out how to make PHP development easy with the latest version of Eclipse, Indigo, and how to set up local and remote debugging.



Ten minutes to PHP objects

PHP is a fully object-oriented language with lots of powerful features. This introductory guide looks at how PHP handles objects.


Other Articles

<ASIN:1847198384>

<ASIN:0596803028>

<ASIN:1430229195>

<ASIN:1430228474>

<ASIN:0596804377>

<ASIN:0596159773>



Last Updated ( Thursday, 30 December 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.