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 https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: /**
  24. 24:  * Standard template helpers shipped with Nette Framework.
  25. 25:  *
  26. 26:  * @author     David Grudl
  27. 27:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  28. 28:  * @package    Nette\Templates
  29. 29:  */
  30. 30: final class TemplateHelpers
  31. 31: {
  32. 32:  
  33. 33:     /**
  34. 34:      * Static class - cannot be instantiated.
  35. 35:      */
  36. 36:     final public function __construct()
  37. 37:     {
  38. 38:         throw new LogicException("Cannot instantiate static class " get_class($this));
  39. 39:     }
  40. 40:  
  41. 41:  
  42. 42:  
  43. 43:     /**
  44. 44:      * Try to load the requested helper.
  45. 45:      * @param  string  helper name
  46. 46:      * @return callback 
  47. 47:      */
  48. 48:     public static function loader($helper)
  49. 49:     {
  50. 50:         $callback 'Nette\Templates\TemplateHelpers::' $helper;
  51. 51:         fixCallback($callback);
  52. 52:         if (is_callable($callback)) {
  53. 53:             return $callback;
  54. 54:         }
  55. 55:         $callback 'Nette\String::' $helper;
  56. 56:         fixCallback($callback);
  57. 57:         if (is_callable($callback)) {
  58. 58:             return $callback;
  59. 59:         }
  60. 60:     }
  61. 61:  
  62. 62:  
  63. 63:  
  64. 64:     /**
  65. 65:      * Escapes string for use inside HTML template.
  66. 66:      * @param  mixed  UTF-8 encoding or 8-bit
  67. 67:      * @return string 
  68. 68:      */
  69. 69:     public static function escapeHtml($s)
  70. 70:     {
  71. 71:         if (is_object($s&& ($s instanceof Template || $s instanceof Html || $s instanceof Form)) {
  72. 72:             return (string) $s;
  73. 73:         }
  74. 74:         return htmlSpecialChars($sENT_QUOTES);
  75. 75:     }
  76. 76:  
  77. 77:  
  78. 78:  
  79. 79:     /**
  80. 80:      * Escapes string for use inside XML 1.0 template.
  81. 81:      * @param  string UTF-8 encoding or 8-bit
  82. 82:      * @return string 
  83. 83:      */
  84. 84:     public static function escapeXML($s)
  85. 85:     {
  86. 86:         // XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
  87. 87:         // 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
  88. 88:         return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#'''$s)ENT_QUOTES);
  89. 89:     }
  90. 90:  
  91. 91:  
  92. 92:  
  93. 93:     /**
  94. 94:      * Escapes string for use inside CSS template.
  95. 95:      * @param  string UTF-8 encoding or 8-bit
  96. 96:      * @return string 
  97. 97:      */
  98. 98:     public static function escapeCss($s)
  99. 99:     {
  100. 100:         // http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
  101. 101:         return addcslashes($s"\x00..\x2C./:;<=>?@[\\]^`{|}~");
  102. 102:     }
  103. 103:  
  104. 104:  
  105. 105:  
  106. 106:     /**
  107. 107:      * Escapes string for use inside HTML style attribute.
  108. 108:      * @param  string UTF-8 encoding or 8-bit
  109. 109:      * @return string 
  110. 110:      */
  111. 111:     public static function escapeHtmlCss($s)
  112. 112:     {
  113. 113:         return htmlSpecialChars(self::escapeCss($s)ENT_QUOTES);
  114. 114:     }
  115. 115:  
  116. 116:  
  117. 117:  
  118. 118:     /**
  119. 119:      * Escapes string for use inside JavaScript template.
  120. 120:      * @param  mixed  UTF-8 encoding
  121. 121:      * @return string 
  122. 122:      */
  123. 123:     public static function escapeJs($s)
  124. 124:     {
  125. 125:         return json_encode($s);
  126. 126:     }
  127. 127:  
  128. 128:  
  129. 129:  
  130. 130:     /**
  131. 131:      * Escapes string for use inside HTML JavaScript attribute.
  132. 132:      * @param  mixed  UTF-8 encoding
  133. 133:      * @return string 
  134. 134:      */
  135. 135:     public static function escapeHtmlJs($s)
  136. 136:     {
  137. 137:         return htmlSpecialChars(json_encode($s)ENT_QUOTES);
  138. 138:     }
  139. 139:  
  140. 140:  
  141. 141:  
  142. 142:     /**
  143. 143:      * Replaces all repeated white spaces with a single space.
  144. 144:      * @param  string UTF-8 encoding or 8-bit
  145. 145:      * @return string 
  146. 146:      */
  147. 147:     public static function strip($s)
  148. 148:     {
  149. 149:         return trim(preg_replace('#\\s+#'' '$s));
  150. 150:     }
  151. 151:  
  152. 152:  
  153. 153:  
  154. 154:     /**
  155. 155:      * Indents the HTML content from the left.
  156. 156:      * @param  string UTF-8 encoding or 8-bit
  157. 157:      * @param  int 
  158. 158:      * @param  string 
  159. 159:      * @return string 
  160. 160:      */
  161. 161:     public static function indent($s$level 1$chars "\t")
  162. 162:     {
  163. 163:         if ($level >= 1{
  164. 164:             $s preg_replace_callback('#<(textarea|pre).*?</\\1#si'array(__CLASS__'indentCb')$s);
  165. 165:             $s String::indent($s$level$chars);
  166. 166:             $s strtr($s"\x1D\x1A""\r\n");
  167. 167:         }
  168. 168:         return $s;
  169. 169:     }
  170. 170:  
  171. 171:  
  172. 172:  
  173. 173:     /**
  174. 174:      * Callback for self::indent
  175. 175:      */
  176. 176:     private static function indentCb($m)
  177. 177:     {
  178. 178:         return strtr($m[0]"\r\n""\x1D\x1A");
  179. 179:     }
  180. 180:  
  181. 181:  
  182. 182:  
  183. 183:     /**
  184. 184:      * Date/time formatting.
  185. 185:      * @param  string|int|DateTime
  186. 186:      * @param  string 
  187. 187:      * @return string 
  188. 188:      */
  189. 189:     public static function date($value$format "%x")
  190. 190:     {
  191. 191:         $value is_numeric($value? (int) $value ($value instanceof DateTime $value->format('U'strtotime($value));
  192. 192:         return strftime($format$value);
  193. 193:     }
  194. 194:  
  195. 195:  
  196. 196:  
  197. 197:     /**
  198. 198:      * Converts to human readable file size.
  199. 199:      * @param  int 
  200. 200:      * @return string 
  201. 201:      */
  202. 202:     public static function bytes($bytes)
  203. 203:     {
  204. 204:         $bytes = (int) $bytes;
  205. 205:         $units array('B''kB''MB''GB''TB''PB');
  206. 206:         foreach ($units as $unit{
  207. 207:             if (abs($bytes1024break;
  208. 208:             $bytes $bytes 1024;
  209. 209:         }
  210. 210:         return round($bytes2' ' $unit;
  211. 211:     }
  212. 212: