Source for file TemplateHelpers.php

Documentation is available at TemplateHelpers.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: /**
  23. 23:  * Standard template run-time helpers shipped with Nette Framework.
  24. 24:  *
  25. 25:  * @author     David Grudl
  26. 26:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  27. 27:  * @package    Nette\Templates
  28. 28:  */
  29. 29: final class TemplateHelpers
  30. 30: {
  31. 31:  
  32. 32:     /**
  33. 33:      * Static class - cannot be instantiated.
  34. 34:      */
  35. 35:     final public function __construct()
  36. 36:     {
  37. 37:         throw new LogicException("Cannot instantiate static class " get_class($this));
  38. 38:     }
  39. 39:  
  40. 40:  
  41. 41:  
  42. 42:     /**
  43. 43:      * Try to load the requested helper.
  44. 44:      * @param  string  helper name
  45. 45:      * @return callback 
  46. 46:      */
  47. 47:     public static function loader($helper)
  48. 48:     {
  49. 49:         $callback 'Nette\Templates\TemplateHelpers::' $helper;
  50. 50:         fixCallback($callback);
  51. 51:         if (is_callable($callback)) {
  52. 52:             return $callback;
  53. 53:         }
  54. 54:         $callback 'Nette\String::' $helper;
  55. 55:         fixCallback($callback);
  56. 56:         if (is_callable($callback)) {
  57. 57:             return $callback;
  58. 58:         }
  59. 59:     }
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Escapes string for use inside HTML template.
  65. 65:      * @param  mixed  UTF-8 encoding or 8-bit
  66. 66:      * @return string 
  67. 67:      */
  68. 68:     public static function escapeHtml($s)
  69. 69:     {
  70. 70:         if (is_object($s&& ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
  71. 71:             return $s->__toString(TRUE);
  72. 72:         }
  73. 73:         return htmlSpecialChars($sENT_QUOTES);
  74. 74:     }
  75. 75:  
  76. 76:  
  77. 77:  
  78. 78:     /**
  79. 79:      * Escapes string for use inside HTML comments.
  80. 80:      * @param  mixed  UTF-8 encoding or 8-bit
  81. 81:      * @return string 
  82. 82:      */
  83. 83:     public static function escapeHtmlComment($s)
  84. 84:     {
  85. 85:         // -- has special meaning in different browsers
  86. 86:         return str_replace('--''--><!--'$s)// HTML tags have no meaning inside comments
  87. 87:     }
  88. 88:  
  89. 89:  
  90. 90:  
  91. 91:     /**
  92. 92:      * Escapes string for use inside XML 1.0 template.
  93. 93:      * @param  string UTF-8 encoding or 8-bit
  94. 94:      * @return string 
  95. 95:      */
  96. 96:     public static function escapeXML($s)
  97. 97:     {
  98. 98:         // XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
  99. 99:         // XML 1.1: \x00 forbidden directly and as a character reference, \x09 \x0A \x0D \x85 allowed directly, C0, C1 and \x7F allowed as character references
  100. 100:         return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#'''$s)ENT_QUOTES);
  101. 101:     }
  102. 102:  
  103. 103:  
  104. 104:  
  105. 105:     /**
  106. 106:      * Escapes string for use inside CSS template.
  107. 107:      * @param  string UTF-8 encoding or 8-bit
  108. 108:      * @return string 
  109. 109:      */
  110. 110:     public static function escapeCss($s)
  111. 111:     {
  112. 112:         // http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
  113. 113:         return addcslashes($s"\x00..\x2C./:;<=>?@[\\]^`{|}~");
  114. 114:     }
  115. 115:  
  116. 116:  
  117. 117:  
  118. 118:     /**
  119. 119:      * Escapes string for use inside HTML style attribute.
  120. 120:      * @param  string UTF-8 encoding or 8-bit
  121. 121:      * @return string 
  122. 122:      */
  123. 123:     public static function escapeHtmlCss($s)
  124. 124:     {
  125. 125:         return htmlSpecialChars(self::escapeCss($s)ENT_QUOTES);
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Escapes string for use inside JavaScript template.
  132. 132:      * @param  mixed  UTF-8 encoding
  133. 133:      * @return string 
  134. 134:      */
  135. 135:     public static function escapeJs($s)
  136. 136:     {
  137. 137:         if (is_object($s&& ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
  138. 138:             $s $s->__toString(TRUE);
  139. 139:         }
  140. 140:         return str_replace(']]>'']]\x3E'json_encode($s));
  141. 141:     }
  142. 142:  
  143. 143:  
  144. 144:  
  145. 145:     /**
  146. 146:      * Escapes string for use inside HTML JavaScript attribute.
  147. 147:      * @param  mixed  UTF-8 encoding
  148. 148:      * @return string 
  149. 149:      */
  150. 150:     public static function escapeHtmlJs($s)
  151. 151:     {
  152. 152:         return htmlSpecialChars(self::escapeJs($s)ENT_QUOTES);
  153. 153:     }
  154. 154:  
  155. 155:  
  156. 156:  
  157. 157:     /**
  158. 158:      * Replaces all repeated white spaces with a single space.
  159. 159:      * @param  string UTF-8 encoding or 8-bit
  160. 160:      * @return string 
  161. 161:      */
  162. 162:     public static function strip($s)
  163. 163:     {
  164. 164:         $s preg_replace_callback('#<(textarea|pre|script).*?</\\1#si'array(__CLASS__'indentCb')$s);
  165. 165:         $s trim(preg_replace('#[ \t\r\n]+#'' '$s));
  166. 166:         return strtr($s"\x1F\x1E\x1D\x1A"" \t\r\n");
  167. 167:     }
  168. 168:  
  169. 169:  
  170. 170:  
  171. 171:     /**
  172. 172:      * Indents the HTML content from the left.
  173. 173:      * @param  string UTF-8 encoding or 8-bit
  174. 174:      * @param  int 
  175. 175:      * @param  string 
  176. 176:      * @return string 
  177. 177:      */
  178. 178:     public static function indent($s$level 1$chars "\t")
  179. 179:     {
  180. 180:         if ($level >= 1{
  181. 181:             $s preg_replace_callback('#<(textarea|pre).*?</\\1#si'array(__CLASS__'indentCb')$s);
  182. 182:             $s String::indent($s$level$chars);
  183. 183:             $s strtr($s"\x1F\x1E\x1D\x1A"" \t\r\n");
  184. 184:         }
  185. 185:         return $s;
  186. 186:     }
  187. 187:  
  188. 188:  
  189. 189:  
  190. 190:     /**
  191. 191:      * Callback for self::indent
  192. 192:      */
  193. 193:     private static function indentCb($m)
  194. 194:     {
  195. 195:         return strtr($m[0]" \t\r\n""\x1F\x1E\x1D\x1A");
  196. 196:     }
  197. 197:  
  198. 198:  
  199. 199:  
  200. 200:     /**
  201. 201:      * Date/time formatting.
  202. 202:      * @param  string|int|DateTime
  203. 203:      * @param  string 
  204. 204:      * @return string 
  205. 205:      */
  206. 206:     public static function date($time$format "%x")
  207. 207:     {
  208. 208:         if ($time == NULL// intentionally ==
  209. 209:             return NULL;
  210. 210:  
  211. 211:         elseif (!($time instanceof DateTime)) {
  212. 212:             $time new DateTime(is_numeric($timedate('Y-m-d H:i:s'$time$time);
  213. 213:         }
  214. 214:  
  215. 215:         return strpos($format'%'=== FALSE
  216. 216:             ? $time->format($format// formats using date()
  217. 217:             : strftime($format$time->format('U'))// formats according to locales
  218. 218:     }
  219. 219:  
  220. 220:  
  221. 221:  
  222. 222:     /**
  223. 223:      * Converts to human readable file size.
  224. 224:      * @param  int 
  225. 225:      * @param  int 
  226. 226:      * @return string 
  227. 227:      */
  228. 228:     public static function bytes($bytes$precision 2)
  229. 229:     {
  230. 230:         $bytes round($bytes);
  231. 231:         $units array('B''kB''MB''GB''TB''PB');
  232. 232:         foreach ($units as $unit{
  233. 233:             if (abs($bytes1024 || $unit === end($units)) break;
  234. 234:             $bytes $bytes 1024;
  235. 235:         }
  236. 236:         return round($bytes$precision' ' $unit;
  237. 237:     }
  238. 238:  
  239. 239:  
  240. 240:  
  241. 241:     /**
  242. 242:      * Returns array of string length.
  243. 243:      * @param  mixed 
  244. 244:      * @return int 
  245. 245:      */
  246. 246:     public static function length($var)
  247. 247:     {
  248. 248:         return is_string($variconv_strlen($var'UTF-8'count($var);
  249. 249:     }
  250. 250:  
  251. 251:  
  252. 252:  
  253. 253:     /**
  254. 254:      * Performs a search and replace.
  255. 255:      * @param  string 
  256. 256:      * @param  string 
  257. 257:      * @param  string 
  258. 258:      * @return string 
  259. 259:      */
  260. 260:     public static function replace($subject$search$replacement '')
  261. 261:     {
  262. 262:         return str_replace($search$replacement$subject);
  263. 263:     }
  264. 264:  
  265. 265:  
  266. 266:  
  267. 267:     /**
  268. 268:      * Performs a regular expression search and replace.
  269. 269:      * @param  string 
  270. 270:      * @param  string 
  271. 271:      * @param  string 
  272. 272:      * @return string 
  273. 273:      */
  274. 274:     public static function replaceRe($subject$pattern$replacement '')
  275. 275:     {
  276. 276:         return preg_replace($pattern$replacement$subject);
  277. 277:     }
  278. 278:  
  279. 279:  
  280. 280:  
  281. 281:     /**
  282. 282:      * /dev/null.
  283. 283:      * @param  mixed 
  284. 284:      * @return string 
  285. 285:      */
  286. 286:     public static function null($value)
  287. 287:     {
  288. 288:         return '';
  289. 289:     }
  290. 290: