File modules/template_engine/element/Element.class.php
Last commit: Sun Nov 15 21:52:19 2020 +0100 Jan Dankert Optimized template element renderer: Elements without children are closed in the same line. This fixes the space problem in HTML textareas.
1 <?php 2 3 4 namespace template_engine\element; 5 6 7 use template_engine\element\XMLFormatter; 8 use template_engine\element\attribute\SimpleAttribute; 9 10 class Element 11 { 12 private $name; 13 /** 14 * @var array 15 */ 16 protected $attributes = []; 17 protected $content = ''; 18 19 protected $selfClosing = false; 20 21 /** 22 * @var array 23 */ 24 protected $children = []; 25 26 27 /** 28 * @param $element Element 29 */ 30 public function asChildOf($element ) { 31 $element->addChild($this); 32 return $this; 33 } 34 35 public function addChild($child ) { 36 37 if ( is_array( $child ) ) 38 $this->children += $child; 39 else 40 $this->children[] = $child; 41 42 return $this; 43 } 44 45 public function __construct( $name ) 46 { 47 $this->name = $name; 48 } 49 50 protected function getAttributeValue($name ){ 51 return $this->attributes[$name]->render(); 52 } 53 54 /** 55 * The textual content of the element. 56 * 57 * @param string $content 58 * @return $this 59 */ 60 public function content( $content ) 61 { 62 $this->content = $content; 63 return $this; 64 } 65 66 /** 67 * @param $format XMLFormatter 68 * @return string 69 */ 70 public function render($format ) { 71 72 $this->selfClosing = $this->selfClosing && !$this->content && !$this->children; 73 74 $content = $format->getIndentation(); 75 76 if ( $this->name ) 77 $content .= '<'.$this->name. 78 array_reduce( array_keys($this->attributes),function($carry,$key){return $carry.' '.$this->getAttributeValue($key);},'').(($this->selfClosing ?' /':'').'>'); 79 80 $content .= $this->getContent(); 81 82 $content .= $this->renderChildren( $format ); 83 84 if ( $this->selfClosing ) 85 ; 86 else 87 if ( $this->name ) { 88 if ( $this->children ) 89 // Elements with children are closed on the next line 90 $content .= $format->getIndentationOnClose(); 91 92 // Closing element 93 $content .= '</'.$this->name.'>'; 94 } 95 96 return $content; 97 } 98 99 public function addAttribute($key, $value) { 100 $this->attributes[] = new SimpleAttribute($key,$value); 101 return $this; 102 } 103 104 public function selfClosing($selfClosing) { 105 $this->selfClosing = boolval($selfClosing); 106 return $this; 107 } 108 109 protected function getContent() 110 { 111 return $this->content; 112 } 113 114 /** 115 * @param $format XMLFormatter 116 * @return string 117 */ 118 protected function renderChildren($format ) 119 { 120 $content = ''; 121 122 /** @var Element $child */ 123 foreach($this->children as $child ) { 124 $content .= $child->render( $format->deeper() ); 125 } 126 127 return $content; 128 } 129 } 130 131
Downloadmodules/template_engine/element/Element.class.php
History Sun, 15 Nov 2020 21:52:19 +0100 Jan Dankert Optimized template element renderer: Elements without children are closed in the same line. This fixes the space problem in HTML textareas. Mon, 5 Oct 2020 23:32:06 +0200 Jan Dankert UI: Nicer buttons Sun, 23 Feb 2020 04:55:35 +0100 Jan Dankert New: Formatter for indenting XML files. Sat, 22 Feb 2020 22:23:01 +0100 Jan Dankert Refactoring: Enable Autoloading, Fix namespace structure. Sat, 22 Feb 2020 01:58:20 +0100 Jan Dankert New: Autoloading of classes in all modules.