Last night I decided to see if I could automate the majority of the work for creating web pages like creating the head remembering to close the head then start the body ... Remmebering back to my C++ classes with constructors and destructors I attempted to create a class in php (I stumbled upon classes in php when I was doing other stuff) following is my php Page class which takes some of the work out of making a page.
A quick example of how to use it.Code:<?php class Page { private $buffer=""; private $finnished_head=false; public $wait=false; private $javascript_functions=array(); private $scripting=false; public function __construct($title="Web Page",$wait=false) {$this->wait=$wait; $this->buffer="<!DOCTYPE html>\n<html>\n<head>\n<meta charset='utf-8'>\n<title>".$title."</title>\n"; if(!$this->wait){$this->write();} } public function write() {print $this->buffer; $this->buffer=""; } public function start_body($body_options="") {if($this->scripting){$this->stop_scripting();} if(!$this->finnished_head) {$this->buffer.="</head>\n<body ".$body_options.">\n"; $this->finnished_head=true; if(!$this->wait){$this->write();} }} public function append($text) {if($this->scripting){$this->stop_scripting();} if(!$this->wait){$this->write();} $this->buffer.=$text; if(!$this->wait){$this->write();} } public function __destruct() {$this->start_body(); $this->buffer.="</body>\n</html>\n"; $this->write(); } private function start_scripting() {$this->buffer.="<script>\n"; $this->scripting=true; } private function stop_scripting() {$this->buffer.="</script>\n"; $this->scripting=false; } public function f($name,$code,$args="") {if($code!="" && $code != null) {if($this->javascript_functions[$name]) {print "Usage error: You tryed over writting a previous declared javascript function"; } else {if(!$this->scripting){$this->start_scripting();} $this->javascript_functions[$name]=true; $this->buffer.="function ".$name."(".$args.")\n{".$code."\n}\n"; if(!$this->wait){$this->write();} }}} } ?>
I'm not done monkeying with it but I really like the results so far. I no longer need to remember to close script sections as my code does that for me. Also it closes the head section when I start the body and closes the html automatically because of the destructor.Code:<?php include "Page.php"; $mypage=new Page(); $mypage->f("test","alert('test');"); $mypage->start_body("onload='test()'"); $mypage->append( "<form> <input name='test' type='text'> </form> "); ?>


Reply With Quote
Bookmarks