Revision: 27402
                            
                                                            
                                    
                                        
Initial Code
                                    
                                    
                                                            
                                    
                                        
Initial URL
                                    
                                    
                                                            
                                    
                                        
Initial Description
                                    
                                    
                                                            
                                    
                                        
Initial Title
                                    
                                    
                                                            
                                    
                                        
Initial Tags
                                    
                                    
                                                            
                                    
                                        
Initial Language
                                    
                                    
                                                    
                        at June 6, 2010 10:00 by minky
                            
                            Initial Code
<?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 $attributes = array();
        private $special = array("img", "input", "hr", "br", "meta", "link");
        public function __construct($element, $innerHTML = NULL, $attributes = NULL) {
            $this->element = $element;
            if(!is_null($innerHTML))
                $this->innerHTML($innerHTML);
            if(!is_null($attributes))
                $this->attributes($attributes);
                
        }
        public function __toString() {
            return $this->generate();
        }
        public function attributes($attributes) {
            $this->attributes = array_merge($this->attributes, (array) $attributes);
            return $this;
        }
        public function innerHTML($innerHTML) {
            
            $this->innerHTML = $innerHTML;
            return $this;
        }
        public function output() {
            return $this->generate();
        }
        
        private function generate() {
            $html = "<{$this->element}";
            if(!empty($this->attributes)) {
                foreach($this->attributes AS $key => $value) {
                    // allow boolean array("disabled" => true);
                    if(is_bool($value)) {
                        // most browsers support <.. disabled OR disabled="disabled" />
                        if(!$value) continue;
                        $value = $key;
                    }
                    $html .= ' '. $key .'="'. $value .'"';
                }
            }
            if(in_array($this->element, $this->special)) {
                $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"/>
    echo new html("a", "Link Text", array("href" => "http://www.google.com"));
    // <a href="http://www.google.com">Link Text</a>
    $html = new html("a");
    $html->innerHTML("Link Text");
    $html->attributes(array("href" => "http://www.google.com"));
    echo $html->output();
    // <a href="http://www.google.com">Link Text</a>
    
    echo $html->innerHTML("Link Text")->attributes(array("href" => "http://www.google.com"))->output();
    // <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>
    
?>
                                Initial URL
http://www.pixaweb.co.uk/resources?tag=php5
Initial Description
## PHP - HTML Generation Class This class allows you to generate (and fill) any HTML elements via programmatic or object-orientated methods
Initial Title
PHP - HTML Generation Class
Initial Tags
php, html
Initial Language
PHP