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