|
TYPO3 API
SVNRelease
|
00001 <?php 00002 00003 /* * 00004 * This script belongs to the FLOW3 package "Fluid". * 00005 * * 00006 * It is free software; you can redistribute it and/or modify it under * 00007 * the terms of the GNU Lesser General Public License as published by the * 00008 * Free Software Foundation, either version 3 of the License, or (at your * 00009 * option) any later version. * 00010 * * 00011 * This script is distributed in the hope that it will be useful, but * 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- * 00013 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser * 00014 * General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with the script. * 00018 * If not, see http://www.gnu.org/licenses/lgpl.html * 00019 * * 00020 * The TYPO3 project - inspiring people to share! * 00021 * */ 00022 00023 /** 00024 * Tag builder. Can be easily accessed in AbstractTagBasedViewHelper 00025 * 00026 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later 00027 * @api 00028 */ 00029 class Tx_Fluid_Core_ViewHelper_TagBuilder { 00030 00031 /** 00032 * Name of the Tag to be rendered 00033 * 00034 * @var string 00035 */ 00036 protected $tagName = ''; 00037 00038 /** 00039 * Content of the tag to be rendered 00040 * 00041 * @var string 00042 */ 00043 protected $content = ''; 00044 00045 /** 00046 * Attributes of the tag to be rendered 00047 * 00048 * @var array 00049 */ 00050 protected $attributes = array(); 00051 00052 /** 00053 * Specifies whether this tag needs a closing tag. 00054 * E.g. <textarea> cant be self-closing even if its empty 00055 * 00056 * @var boolean 00057 */ 00058 protected $forceClosingTag = FALSE; 00059 00060 /** 00061 * Constructor 00062 * 00063 * @param string $tagName name of the tag to be rendered 00064 * @param string $tagContent content of the tag to be rendered 00065 * @author Bastian Waidelich <bastian@typo3.org> 00066 * @api 00067 */ 00068 public function __construct($tagName = '', $tagContent = '') { 00069 $this->setTagName($tagName); 00070 $this->setContent($tagContent); 00071 } 00072 00073 /** 00074 * Sets the tag name 00075 * 00076 * @param string $tagName name of the tag to be rendered 00077 * @return void 00078 * @author Bastian Waidelich <bastian@typo3.org> 00079 * @api 00080 */ 00081 public function setTagName($tagName) { 00082 $this->tagName = $tagName; 00083 } 00084 00085 /** 00086 * Gets the tag name 00087 * 00088 * @return string tag name of the tag to be rendered 00089 * @author Bastian Waidelich <bastian@typo3.org> 00090 * @api 00091 */ 00092 public function getTagName() { 00093 return $this->tagName; 00094 } 00095 00096 /** 00097 * Sets the content of the tag 00098 * 00099 * @param string $tagContent content of the tag to be rendered 00100 * @return void 00101 * @author Bastian Waidelich <bastian@typo3.org> 00102 * @api 00103 */ 00104 public function setContent($tagContent) { 00105 $this->content = $tagContent; 00106 } 00107 00108 /** 00109 * Gets the content of the tag 00110 * 00111 * @return string content of the tag to be rendered 00112 * @author Bastian Waidelich <bastian@typo3.org> 00113 * @api 00114 */ 00115 public function getContent() { 00116 return $this->content; 00117 } 00118 00119 /** 00120 * Returns TRUE if tag contains content, otherwise FALSE 00121 * 00122 * @return boolean TRUE if tag contains text, otherwise FALSE 00123 * @author Bastian Waidelich <bastian@typo3.org> 00124 * @api 00125 */ 00126 public function hasContent() { 00127 if ($this->content === NULL) { 00128 return FALSE; 00129 } 00130 return $this->content !== ''; 00131 } 00132 00133 /** 00134 * Set this to TRUE to force a closing tag 00135 * E.g. <textarea> cant be self-closing even if its empty 00136 * 00137 * @param boolean $forceClosingTag 00138 * @author Bastian Waidelich <bastian@typo3.org> 00139 * @api 00140 */ 00141 public function forceClosingTag($forceClosingTag) { 00142 $this->forceClosingTag = $forceClosingTag; 00143 } 00144 00145 /** 00146 * Adds an attribute to the $attributes-collection 00147 * 00148 * @param string $attributeName name of the attribute to be added to the tag 00149 * @param string $attributeValue attribute value 00150 * @param boolean $escapeSpecialCharacters apply htmlspecialchars to attribute value 00151 * @return void 00152 * @author Bastian Waidelich <bastian@typo3.org> 00153 * @api 00154 */ 00155 public function addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters = TRUE) { 00156 if ($escapeSpecialCharacters) { 00157 $attributeValue = htmlspecialchars($attributeValue); 00158 } 00159 $this->attributes[$attributeName] = $attributeValue; 00160 } 00161 00162 /** 00163 * Adds attributes to the $attributes-collection 00164 * 00165 * @param array $attributes collection of attributes to add. key = attribute name, value = attribute value 00166 * @param boolean $escapeSpecialCharacters apply htmlspecialchars to attribute values# 00167 * @return void 00168 * @author Bastian Waidelich <bastian@typo3.org> 00169 * @api 00170 */ 00171 public function addAttributes(array $attributes, $escapeSpecialCharacters = TRUE) { 00172 foreach($attributes as $attributeName => $attributeValue) { 00173 $this->addAttribute($attributeName, $attributeValue, $escapeSpecialCharacters); 00174 } 00175 } 00176 00177 /** 00178 * Removes an attribute from the $attributes-collection 00179 * 00180 * @param string $attributeName name of the attribute to be removed from the tag 00181 * @return void 00182 * @author Bastian Waidelich <bastian@typo3.org> 00183 * @api 00184 */ 00185 public function removeAttribute($attributeName) { 00186 unset($this->attributes[$attributeName]); 00187 } 00188 00189 /** 00190 * Resets the TagBuilder by setting all members to their default value 00191 * 00192 * @return void 00193 * @author Bastian Waidelich <bastian@typo3.org> 00194 * @api 00195 */ 00196 public function reset() { 00197 $this->tagName = ''; 00198 $this->content = ''; 00199 $this->attributes = array(); 00200 $this->forceClosingTag = FALSE; 00201 } 00202 00203 /** 00204 * Renders and returns the tag 00205 * 00206 * @return void 00207 * @author Bastian Waidelich <bastian@typo3.org> 00208 * @api 00209 */ 00210 public function render() { 00211 if (empty($this->tagName)) { 00212 return ''; 00213 } 00214 $output = '<' . $this->tagName; 00215 foreach($this->attributes as $attributeName => $attributeValue) { 00216 $output .= ' ' . $attributeName . '="' . $attributeValue . '"'; 00217 } 00218 if ($this->hasContent() || $this->forceClosingTag) { 00219 $output .= '>' . $this->content . '</' . $this->tagName . '>'; 00220 } else { 00221 $output .= ' />'; 00222 } 00223 return $output; 00224 } 00225 } 00226 ?>
1.8.0