/ Published in: PHP
## PHP - HTML Generation Class
This class allows you to generate (and fill) any HTML elements via programmatic or object-orientated methods
This class allows you to generate (and fill) any HTML elements via programmatic or object-orientated methods
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php /** * html * * @copyright Copyright 2010 (c) Jared Clarke @ Pixaweb.co.uk * @author Jared Clarke <[email protected]> * @version 0.1 */ class html { public $element; public $innerHTML; public function __construct($element, $innerHTML = NULL, $attributes = NULL) { $this->element = $element; $this->innerHTML($innerHTML); $this->attributes($attributes); } public function __toString() { return $this->generate(); } public function attributes($attributes) { return $this; } public function innerHTML($innerHTML) { $this->innerHTML = $innerHTML; return $this; } public function output() { return $this->generate(); } private function generate() { $html = "<{$this->element}"; foreach($this->attributes AS $key => $value) { // allow boolean array("disabled" => true); // most browsers support <.. disabled OR disabled="disabled" /> if(!$value) continue; $value = $key; } $html .= ' '. $key .'="'. $value .'"'; } } $html .= "/>"; return $html; } $html .= ">{$this->innerHTML}</{$this->element}>"; return $html; } } $input = new html("input"); echo $input->attributes(array("name" => "test", "value" => "testing", "disabled" => true))->output(); // <input name="test" value="testing" disabled="disabled"/> // <a href="http://www.google.com">Link Text</a> $html = new html("a"); $html->innerHTML("Link Text"); echo $html->output(); // <a href="http://www.google.com">Link Text</a> // <a href="http://www.google.com">Link Text</a> $html->innerHTML = "Override Text"; echo $html->output(); // <a href="http://www.google.com">Override Text</a> $html->attributes["href"] = "http://www.yahoo.com"; echo $html->output(); // <a href="http://www.yahoo.com">Override Text</a> ?>
URL: http://www.pixaweb.co.uk/resources?tag=php5