OO Meeting - HTML Writer class

4 views
Skip to first unread message

Jason

unread,
May 12, 2007, 11:27:27 PM5/12/07
to Central Florida PHP
Here is my attempt at the HTML writer classes. It could use some more
work if anyone wants to take a hack at it. Please let me know what you
all think.

HTMLWriter.class.php
<?php
/*********************************************************
* Author: Jason Houle
* ja...@jasonhoule.com
* File: HTMLWriter.class.php
* Desc: This class will assist in creating HTML markup
**********************************************************/

require('Markup.class.php');

class HTMLWriter {
private $doc;
private $html;
private $body;
static public $DOCTYPE = '<!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML
1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';

/
***************************************************************************************
* Constructor
* Params: pageTitle = The title to display
* includes = An array of files to be included in the header.
* For example array("/resources/js/script.js", "/styles/
myStyles.css");
* Return: An HTMLWriter object

****************************************************************************************/
public function __construct($pageTitle, $includes=NULL) {
self::writeDocType();
try {
// Create the document object
$this->doc = new DOMDocument();
} catch (Exception $e) {
echo "Could not create document: $e";
}
$this->html = $this->doc->createElement('html');
$head = $this->doc->createElement('head');
$title = $this->doc->createElement('title', $pageTitle);
$head->appendChild($title);
/***************************************************
* check to see if we have any files to be included
* i.e. JavaScript or CSS. Can be passed as an array
* or a String
***************************************************/
if($includes != NULL) {
if(is_array($includes)) {
foreach($includes as $value) {
$head->appendChild($this->includeFile($value));
}
} else {
$head->appendChild($this->includeFile($includes));
}
}
$this->html->appendChild($head);
$this->body = $this->doc->createElement('body');
}

// Wrap everything up
function __destruct() {
$this->html->appendChild($this->body);
$this->doc->appendChild($this->html);
$this->write();
}

// Write out the HTML
private function write() {
echo $this->doc->saveHTML();
}

// Write the DocType
public static function writeDocType() {
echo self::$DOCTYPE;
}

/
*************************************************************************
* createElement
* params: name = The type of element to create
* data = The data to be displayed in this element
* attr = A hash map of attributes and their values
* i.e. array('class'=>'nav','style'=>'text-decoration:none')
* return: void

**************************************************************************/
function createElement($name, $data=NULL, $attr=NULL) {
$elmnt = Markup::createElement($this->doc, $name, $data, $attr);
$this->body->appendChild($elmnt);
}

/**************************************************
* includeFile
* params: file = The path to the file to include
* return: A DOMNode object
**************************************************/
function includeFile($file) {
$inc = NULL;
if(strripos($file, ".js") > -1) {
$inc = $this->doc->createElement('script');
$inc->setAttribute('src',$file);
$inc->setAttribute('type','text/javascript');
} else if(strripos($file, ".css") > -1) {
$inc = $this->doc->createElement('link');
$inc->setAttribute('href', $file);
$inc->setAttribute('rel', 'stylesheet');
$inc->setAttribute('type', 'text/css');
}
return $inc;
}
}
?>


Markup.class.php
<?php
/*********************************************************
* Author: Jason Houle
* ja...@jasonhoule.com
* File: Markup.class.php
* Desc: This class will assist in creating HTML markup
**********************************************************/

class Markup {

/************************************************************
* createElement
* params: DOMDocument = Pass the DOM object that we are
* currently working with
* name = The type of element to create
* data = The data to be displayed in this element
* attr = The attributes for this element
* return A new Element object
*************************************************************/
static function createElement($domObject, $name, $data=NULL,
$attr=NULL) {
$elmnt;
try {
if(is_array($data)) {
$elmnt = $domObject->createElement($name);
$sub = $data['element'];
for($i=0; $i<count($data)-1; $i++) {
$item = $domObject->createElement($sub, $data[$i]);
$elmnt->appendChild($item);
}
} else {
$elmnt = $domObject->createElement($name, $data);
}
if($attr != NULL) {
foreach($attr as $key=>$value) {
$elmnt->setAttribute($key, $value);
}
}
} catch (Exception $e) {
echo "Could not create $name element: $e";
}
return $elmnt;
}
}
?>

Example.php
<?php
require($_SERVER['DOCUMENT_ROOT']."/HTMLHelper/
HTMLWriter.class.php");

// include my js and css files either as an array or a string
$includes = array("/resources/js/script.js", "/styles/myStyles.css");
//$includes = "/resources/js/script.js";
$html = new HTMLWriter('My HTML writer',$includes);
$html->createElement('h4', 'View the source to see the magic');
$html->createElement('div', 'Here is a &lt;div&gt; element',
array('class'=>'myClass', 'style'=>'text-align:left'));
$html->createElement('h1', 'This is an &lt;h1&gt;');
$html->createElement('img', NULL, array('src'=>'Leilani.jpg',
'border'=>'1', 'alt'=>'Woohoo! An image!', 'title'=>'Woohoo! An
image!'));
$html->createElement('BR');
$html->createElement('span', 'This is a &lt;span&gt; and between here
and');
$html->createElement('BR');
$html->createElement('span', 'here is a &lt;br/&gt;');
$html->createElement('BR');
$html->createElement('a', 'Check out one of my sites!',
array('href'=>'http://TheWeddingVendor.com', 'style'=>'text-
decoration:none'));

// If we are going to create a list then we need to send an array
with 'element'=>'li' as one of the values
$li = array('element'=>'li','Here is an un-ordered list','This is the
second item','And this is the third');
$html->createElement('ul', $li);

// We can even nest elements this way if we want to
// Maybe we can add functionality to improve this
$div = array('element'=>'div','test 1', 'test 2', 'test 3');
$html->createElement('div', $div, array('name'=>'container'));

// Unfortunately this does not work. Maybe someone can fix this.
$html->createElement('div', $html->createElement('div', 'This one
should be nested', array('id'=>'nestedDiv')),
array('id'=>'outerDiv'));

?>

Mike G.

unread,
May 17, 2007, 2:37:41 AM5/17/07
to Central Florida PHP
This is a nice implementation of PHP5's new and improved DOM functions
(http://us.php.net/manual/en/ref.dom.php). I'm happy to see you really
digging into this..

What else does everyone else have?

On May 12, 11:27 pm, Jason <JasonAHo...@gmail.com> wrote:
> Here is my attempt at the HTML writer classes. It could use some more
> work if anyone wants to take a hack at it. Please let me know what you
> all think.
>
> HTMLWriter.class.php
> <?php
> /*********************************************************
> * Author: Jason Houle

> * j...@jasonhoule.com

> * j...@jasonhoule.com

Reply all
Reply to author
Forward
0 new messages