Object-based (X)HTML rendering: more (X)HTML widgets to define - PHP
In this second part of the series, you will learn the basics of object-oriented web page generation through the use of (X)HTML widgets. You will also see how objects implement the “HTMLRenderer” interface to explicitly define its functionality by using the “toHTML()” method.
Once the first "Table" widget has been defined and studied in detail, explaining the logic of the remaining (X)HTML widgets is a fairly easy thing. As expected, divs, paragraphs, or lists implement the same "HTMLRenderer" interface, which directly implies the existence of a concrete definition for the "toHTML()" method. With this concept in mind, I'll continue defining a few more widget classes, starting with the "Div" class:
// class Div class Div implements HTMLRenderer{ private $output='<div '; private $data; private $attributes=array(); public function __construct($attributes=array()){ $this->attributes=$attributes; } public function setData($data){ $this->data=$data; } public function toHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=($this->data instanceof HTMLRenderer)?$this->data->toHTML():$this->data; $this->output.='</div>'; return $this->output; } }
Closely similar to the "Table" class, the above "Div" class exposes the same methods for assigning class properties, in conjunction with the core "toHTML()" method. In this case, the method also defines a recursive implementation of the "HTMLRenderer" interface, so building nested elements is possible by checking the type of object passed through the "instance of" operator. As discussed before, the same logic is applied to each widget class, so understanding them is quite simple.
Now, let's define a few more (X)HTML widget classes, in this case for rendering paragraphs and HTML lists. Here are the corresponding classes:
// class Paragraph class Paragraph implements HTMLRenderer{ private $output='<p '; private $data; private $attributes=array(); public function __construct($attributes=array()){ $this->attributes=$attributes; } public function setData($data){ $this->data=$data; } public function toHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); $this->output.=($this->data instanceof HTMLRenderer)?$this->data->toHTML():$this->data; $this->output.='</p>'; return $this->output; } }
// class HTMLlist class HTMLlist implements HTMLRenderer{ private $output='<ul '; private $data=array(); private $attributes=array(); public function __construct($attributes=array()){ $this->attributes=$attributes; } public function setData($data=array()){ if(count($data)<1){ throw new Exception('Empty data set'); } $this->data=$data; } public function toHTML(){ foreach($this->attributes as $attribute=>$value){ $this->output.=$attribute.'="'.$value.'" '; } $this->output=substr_replace($this->output,'>',-1); foreach($this->data as $data){ $data=($data instanceof HTMLRenderer)?$data->toHTML():$data; $this->output.='<li>'.$data.'</li>'; } $this->output.='</ul>'; return $this->output; } }
Certainly, the above defined classes looks very similar to the previous ones, thus they implement the same "HTMLRenderer" interface. Particularly, the first one takes care of rendering paragraphs, while the second one is tasked with displaying unordered lists. Again, I put strong emphasis on the specific implementation of the "toHTML()" method for both classes.
The next step consists of listing the remaining (X)HTML widgets, by defining the corresponding classes for displaying headers and forms.