login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Handige scripts > htmlTag

htmlTag

Auteur: jvs - 30 mei 2009 - 16:19 - Gekeurd door: Koen - Hits: 2044 - Aantal punten: 4.50 (2 stemmen)



Deze class stelt een htmltag voor. Deze is gemakkelijk te implementeren in ander scripts en laat toe gemakkelijk html elementen te creëren en te wijzigen (attributen toe te voegen/wijzigen). Ik heb deze class bijvoorbeeld gebruik om formulier elementen te renderen in mijn form class. Veel succes ermee!

Code:
  1. <?
  2. // begin class
  3.  
  4. class htmlTag{
  5. /*
  6. @var bool
  7. */
  8. private $shortTag;
  9. /*
  10. @var string
  11. */
  12. private $text;
  13. /*
  14. @var string
  15. */
  16. protected $tagName;
  17. /*
  18. @var array
  19. */
  20. protected $attributes;
  21. /*
  22. @var array
  23. */
  24. protected $childs;
  25.  
  26.  
  27. /*
  28. instantie aanmaken
  29. @param string naam van het object bv: strong, a, li, input, enz...
  30.  
  31. */
  32. public function __construct($iTagName = NULL){
  33.  
  34. if(isset($iTagName)){
  35. $this -> tagName = (string) $iTagName;
  36. }
  37. $this -> attributes = array();
  38. $this -> childs = array();
  39. }
  40.  
  41. /*
  42. @method tellen van het aantal childs
  43. */
  44. public function countChilds(){
  45.  
  46. return count($this -> childs);
  47.  
  48. }
  49.  
  50. public function getChilds(){
  51.  
  52. return $this -> childs;
  53.  
  54. }
  55.  
  56. /*
  57. @method atribuut toevoegen of aanpassen
  58. @param string naam van het atribuut bv: href, value, class
  59. @param string value
  60. */
  61. public function setAttribute($iName, $iValue){
  62.  
  63. $this -> attributes[$iName] = (string) $iValue;
  64.  
  65. }
  66.  
  67. public function getAttribute($iName){
  68.  
  69. if(array_key_exists($iName, $this -> attributes)){
  70.  
  71. return $this -> attributes[$iName];
  72.  
  73. }
  74.  
  75. }
  76.  
  77. /*
  78. @method tagnaam aanpassen
  79. */
  80.  
  81. public function setTagName($iTagName){
  82.  
  83. $this -> tagName = (string) $iTagName;
  84.  
  85. }
  86.  
  87.  
  88. /*
  89. @method child toevoegen
  90. @param htmlTag child moet van het type htmlTag zijn
  91. */
  92. public function addChild(htmlTag $iChild){
  93.  
  94. $this -> childs[] = $iChild;
  95.  
  96. }
  97.  
  98. /*
  99. @method meerdere child in één array toevoegen
  100. @param array
  101. */
  102. public function addRange(array $iChilds){
  103.  
  104. foreach($iChilds as $child){
  105.  
  106. $this -> addChild($child);
  107.  
  108. }
  109.  
  110. }
  111. /*
  112. @method text zetten <b> text </b>
  113. */
  114. public function setText($iText){
  115.  
  116. $this -> text = (string) $iText;
  117.  
  118. }
  119.  
  120.  
  121. public function addText($iText){
  122.  
  123. $this -> text .= (string) $iText;
  124.  
  125. }
  126.  
  127. /*
  128. @method omzetten naar het type korte tag dus zonder afsluitende tag
  129. */
  130. public function setToShortTag(){
  131.  
  132. $this -> shortTag = true;
  133.  
  134. }
  135.  
  136. /*
  137. @method renderen van het html gedeelte
  138. */
  139.  
  140. public function render(){
  141.  
  142. $output = '<';
  143. $output .= $this -> tagName;
  144. // alle atributen aflopen en renderen
  145. foreach($this -> attributes as $key => $value){
  146.  
  147. $output .= ' '.$key. '="' . $value . '"';
  148. }
  149. // korte tag hier afsluiten...
  150. if($this -> shortTag){
  151. $output .= '/>';
  152. }else{
  153.  
  154. $output .= '>';
  155. $output .= "\n";
  156. // text laden
  157. if(isset($this -> text)){
  158. $output .= $this -> text;
  159. }
  160.  
  161. // childs aflopen
  162. foreach($this -> childs as $child){
  163. // childs op hun beurt renderen
  164. $output .= $child -> render();
  165.  
  166. }
  167.  
  168. // afsluitende tag renderen
  169. $output .= '</' . $this -> tagName .'>';
  170.  
  171. }
  172.  
  173. // output terug geven
  174. return $output . "\n";
  175. }
  176.  
  177.  
  178. }
  179.  
  180. // end class
  181.  
  182.  
  183. ?>



voorbeeld:

  1. $list = new htmlTag('ul');
  2. $listItem = new htmlTag('li');
  3.  
  4. $listItem -> setText('ik ben een item');
  5. $listItem -> setAttribute('class', 'test');
  6.  
  7. $list -> addChild($listItem);
  8.  
  9. echo $list -> render();
Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (3)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.039s