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:         return preg_replace_callback(
  155. 155:             '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|$)#si',
  156. 156:             create_function('$m''return trim(preg_replace("#[ \t\r\n]+#", " ", $m[0]));'),
  157. 157:             $s
  158. 158:         );
  159. 159:     }
  160. 160:  
  161. 161:  
  162. 162:  
  163. 163:     /**
  164. 164:      * Indents the HTML content from the left.
  165. 165:      * @param  string UTF-8 encoding or 8-bit
  166. 166:      * @param  int 
  167. 167:      * @param  string 
  168. 168:      * @return string 
  169. 169:      */
  170. 170:     public static function indent($s$level 1$chars "\t")
  171. 171:     {
  172. 172:         if ($level >= 1{
  173. 173:             $s preg_replace_callback('#<(textarea|pre).*?</\\1#si'create_function('$m''return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");')$s);
  174. 174:             $s String::indent($s$level$chars);
  175. 175:             $s strtr($s"\x1F\x1E\x1D\x1A"" \t\r\n");
  176. 176:         }
  177. 177:         return $s;
  178. 178:     }
  179. 179:  
  180. 180:  
  181. 181:  
  182. 182:     /**
  183. 183:      * Date/time formatting.
  184. 184:      * @param  string|int|DateTime
  185. 185:      * @param  string 
  186. 186:      * @return string 
  187. 187:      */
  188. 188:     public static function date($time$format "%x")
  189. 189:     {
  190. 190:         if ($time == NULL// intentionally ==
  191. 191:             return NULL;
  192. 192:         }
  193. 193:  
  194. 194:         $time Tools::createDateTime($time);
  195. 195:         return strpos($format'%'=== FALSE
  196. 196:             ? $time->format($format// formats using date()
  197. 197:             : strftime($format$time->format('U'))// formats according to locales
  198. 198:     }
  199. 199:  
  200. 200:  
  201. 201:  
  202. 202:     /**
  203. 203:      * Converts to human readable file size.
  204. 204:      * @param  int 
  205. 205:      * @param  int 
  206. 206:      * @return string 
  207. 207:      */
  208. 208:     public static function bytes($bytes$precision 2)
  209. 209:     {
  210. 210:         $bytes round($bytes);
  211. 211:         $units array('B''kB''MB''GB''TB''PB');
  212. 212:         foreach ($units as $unit{
  213. 213:             if (abs($bytes1024 || $unit === end($units)) break;
  214. 214:             $bytes $bytes 1024;
  215. 215:         }
  216. 216:         return round($bytes$precision' ' $unit;
  217. 217:     }
  218. 218:  
  219. 219:  
  220. 220:  
  221. 221:     /**
  222. 222:      * Returns array of string length.
  223. 223:      * @param  mixed 
  224. 224:      * @return int 
  225. 225:      */
  226. 226:     public static function length($var)
  227. 227:     {
  228. 228:         return is_string($variconv_strlen($var'UTF-8'count($var);
  229. 229:     }
  230. 230:  
  231. 231:  
  232. 232:  
  233. 233:     /**
  234. 234:      * Performs a search and replace.
  235. 235:      * @param  string 
  236. 236:      * @param  string 
  237. 237:      * @param  string 
  238. 238:      * @return string 
  239. 239:      */
  240. 240:     public static function replace($subject$search$replacement '')
  241. 241:     {
  242. 242:         return str_replace($search$replacement$subject);
  243. 243:     }
  244. 244:  
  245. 245:  
  246. 246:  
  247. 247:     /**
  248. 248:      * Performs a regular expression search and replace.
  249. 249:      * @param  string 
  250. 250:      * @param  string 
  251. 251:      * @param  string 
  252. 252:      * @return string 
  253. 253:      */
  254. 254:     public static function replaceRe($subject$pattern$replacement '')
  255. 255:     {
  256. 256:         return preg_replace($pattern$replacement$subject);
  257. 257:     }
  258. 258:  
  259. 259:  
  260. 260:  
  261. 261:     /**
  262. 262:      * /dev/null.
  263. 263:      * @param  mixed 
  264. 264:      * @return string 
  265. 265:      */
  266. 266:     public static function null($value)
  267. 267:     {
  268. 268:         return '';
  269. 269:     }
  270. 270: