PHP - HTML Generation Class


/ Published in: PHP
Save to your folder(s)

## PHP - HTML Generation Class

This class allows you to generate (and fill) any HTML elements via programmatic or object-orientated methods


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.   * html
  4.   *
  5.   * @copyright Copyright 2010 (c) Jared Clarke @ Pixaweb.co.uk
  6.   * @author Jared Clarke <[email protected]>
  7.   * @version 0.1
  8.   */
  9. class html {
  10.  
  11. public $element;
  12. public $innerHTML;
  13. public $attributes = array();
  14.  
  15. private $special = array("img", "input", "hr", "br", "meta", "link");
  16.  
  17. public function __construct($element, $innerHTML = NULL, $attributes = NULL) {
  18.  
  19. $this->element = $element;
  20.  
  21. if(!is_null($innerHTML))
  22. $this->innerHTML($innerHTML);
  23.  
  24. if(!is_null($attributes))
  25. $this->attributes($attributes);
  26.  
  27. }
  28.  
  29. public function __toString() {
  30. return $this->generate();
  31. }
  32.  
  33. public function attributes($attributes) {
  34.  
  35. $this->attributes = array_merge($this->attributes, (array) $attributes);
  36.  
  37. return $this;
  38.  
  39. }
  40.  
  41. public function innerHTML($innerHTML) {
  42.  
  43. $this->innerHTML = $innerHTML;
  44.  
  45. return $this;
  46.  
  47. }
  48.  
  49. public function output() {
  50. return $this->generate();
  51. }
  52.  
  53. private function generate() {
  54.  
  55. $html = "<{$this->element}";
  56.  
  57. if(!empty($this->attributes)) {
  58. foreach($this->attributes AS $key => $value) {
  59. // allow boolean array("disabled" => true);
  60. if(is_bool($value)) {
  61. // most browsers support <.. disabled OR disabled="disabled" />
  62. if(!$value) continue;
  63.  
  64. $value = $key;
  65.  
  66. }
  67.  
  68. $html .= ' '. $key .'="'. $value .'"';
  69.  
  70. }
  71. }
  72.  
  73. if(in_array($this->element, $this->special)) {
  74.  
  75. $html .= "/>";
  76.  
  77. return $html;
  78.  
  79. }
  80.  
  81. $html .= ">{$this->innerHTML}</{$this->element}>";
  82.  
  83. return $html;
  84.  
  85. }
  86.  
  87. }
  88.  
  89. $input = new html("input");
  90. echo $input->attributes(array("name" => "test", "value" => "testing", "disabled" => true))->output();
  91. // <input name="test" value="testing" disabled="disabled"/>
  92.  
  93. echo new html("a", "Link Text", array("href" => "http://www.google.com"));
  94. // <a href="http://www.google.com">Link Text</a>
  95.  
  96. $html = new html("a");
  97. $html->innerHTML("Link Text");
  98. $html->attributes(array("href" => "http://www.google.com"));
  99. echo $html->output();
  100. // <a href="http://www.google.com">Link Text</a>
  101.  
  102. echo $html->innerHTML("Link Text")->attributes(array("href" => "http://www.google.com"))->output();
  103. // <a href="http://www.google.com">Link Text</a>
  104.  
  105. $html->innerHTML = "Override Text";
  106. echo $html->output();
  107. // <a href="http://www.google.com">Override Text</a>
  108.  
  109. $html->attributes["href"] = "http://www.yahoo.com";
  110. echo $html->output();
  111. // <a href="http://www.yahoo.com">Override Text</a>
  112.  
  113. ?>

URL: http://www.pixaweb.co.uk/resources?tag=php5

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.