note 92947 added to class.domdocument

From: Date: Sat, 15 Aug 2009 20:32:11 +0000
Subject: note 92947 added to class.domdocument
Groups: php.notes 
Request: Send a blank email to php-notes+get-159532@lists.php.net to get a copy of this message
If you want to use the DOMDocument to create xHTML documents here is a simple class Note this is designed for creating xHTML documents from scratch but could be easily extended to work with xHTML documents. Also this is for xHTML not XML. <?php class Document { public $doctype; public $head; public $title = 'Sensei Ninja'; public $body; private $styles; private $metas; private $scripts; private $document; function __construct ( ) { $this->document = new DOMDocument( ); $this->head = $this->document->createElement( 'head', ' ' ); $this->body = $this->document->createElement( 'body', ' ' ); } public function addStyleSheet ( $url, $media='all' ) { $element = $this->document->createElement( 'link' ); $element->setAttribute( 'type', 'text/css' ); $element->setAttribute( 'href', $url ); $element->setAttribute( 'media', $media ); $this->styles[] = $element; } public function addScript ( $url ) { $element = $this->document->createElement( 'script', ' ' ); $element->setAttribute( 'type', 'text/javascript' ); $element->setAttribute( 'src', $url ); $this->scripts[] = $element; } public function addMetaTag ( $name, $content ) { $element = $this->document->createElement( 'meta' ); $element->setAttribute( 'name', $name ); $element->setAttribute( 'content', $content ); $this->metas[] = $element; } public function setDescription ( $dec ) { $this->addMetaTag( 'description', $dec ); } public function setKeywords ( $keywords ) { $this->addMetaTag( 'keywords', $keywords ); } public function createElement ( $nodeName, $nodeValue=null ) { return $this->document->createElement( $nodeName, $nodeValue ); } public function assemble ( ) { // Doctype creation $doctype = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML TRANSITIONAL 1.0//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'; // Create the head element $title = $this->document->createElement( 'title', $this->title ); // Add stylesheets if needed if ( is_array( $this->styles )) foreach ( $this->styles as $element ) $this->head->appendChild( $element ); // Add scripts if needed if( is_array( $this->scripts )) foreach ( $this->scripts as $element ) $this->head->appendChild( $element ); // Add meta tags if needed if ( is_array( $this->metas )) foreach ( $this->metas as $element ) $this->head->appendChild( $element ); $this->head->appendChild( $title ); // Create the document $html = $this->document->createElement( 'html' ); $html->setAttribute( 'xmlns', 'http://www.w3.org/1999/xhtml' ); $html->setAttribute( 'xml:lang', 'en' ); $html->setAttribute( 'lang', 'en' ); $html->appendChild( $this->head ); $html->appendChild( $this->body ); $this->document->appendChild( $html ); return $doctype . $this->document->saveXML( ); } } ?> Small example <?php $document = new Document( ); $document->title = 'Hello'; $document->addStyleSheet( 'StyleSheets/main.css' ); $div = $document->createElement( 'div' ); $div->nodeValue = 'Hello, world!'; $div->setAttribute( 'style', 'color: red;' ); $document->body->appendChild( $div ); printf( '%s', $document->assemble( ) ); ?> ---- Server IP: 208.69.120.35 Probable Submitter: 97.81.197.116 ---- Manual Page -- http://www.php.net/manual/en/class.domdocument.php Edit -- https://master.php.net/note/edit/92947 Del: integrated -- https://master.php.net/note/delete/92947/integrated Del: useless -- https://master.php.net/note/delete/92947/useless Del: bad code -- https://master.php.net/note/delete/92947/bad+code Del: spam -- https://master.php.net/note/delete/92947/spam Del: non-english -- https://master.php.net/note/delete/92947/non-english Del: in docs -- https://master.php.net/note/delete/92947/in+docs Del: other reasons-- https://master.php.net/note/delete/92947 Reject -- https://master.php.net/note/reject/92947 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#159532) next »