1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Nette\Templates;
13:
14: use Nette,
15: Nette\Forms\Form,
16: Nette\Web\Html;
17:
18:
19:
20: 21: 22: 23: 24:
25: final class TemplateHelpers
26: {
27:
28: 29: 30:
31: final public function __construct()
32: {
33: throw new \LogicException("Cannot instantiate static class " . get_class($this));
34: }
35:
36:
37:
38: 39: 40: 41: 42:
43: public static function loader($helper)
44: {
45: $callback = callback('Nette\Templates\TemplateHelpers', $helper);
46: if ($callback->isCallable()) {
47: return $callback;
48: }
49: $callback = callback('Nette\String', $helper);
50: if ($callback->isCallable()) {
51: return $callback;
52: }
53: }
54:
55:
56:
57: 58: 59: 60: 61:
62: public static function escapeHtml($s)
63: {
64: if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
65: return $s->__toString(TRUE);
66: }
67: return htmlSpecialChars($s, ENT_QUOTES);
68: }
69:
70:
71:
72: 73: 74: 75: 76:
77: public static function escapeHtmlComment($s)
78: {
79:
80: return str_replace('--', '--><!-- ', $s);
81: }
82:
83:
84:
85: 86: 87: 88: 89:
90: public static function escapeXML($s)
91: {
92:
93:
94: return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
95: }
96:
97:
98:
99: 100: 101: 102: 103:
104: public static function escapeCss($s)
105: {
106:
107: return addcslashes($s, "\x00..\x1F!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~");
108: }
109:
110:
111:
112: 113: 114: 115: 116:
117: public static function escapeHtmlCss($s)
118: {
119: return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
120: }
121:
122:
123:
124: 125: 126: 127: 128:
129: public static function escapeJs($s)
130: {
131: if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
132: $s = $s->__toString(TRUE);
133: }
134: return str_replace(']]>', ']]\x3E', json_encode($s));
135: }
136:
137:
138:
139: 140: 141: 142: 143:
144: public static function escapeHtmlJs($s)
145: {
146: return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
147: }
148:
149:
150:
151: 152: 153: 154: 155:
156: public static function strip($s)
157: {
158: return preg_replace_callback(
159: '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|$)#si',
160: create_function('$m', 'return trim(preg_replace("#[ \t\r\n]+#", " ", $m[0]));'),
161: $s
162: );
163: }
164:
165:
166:
167: 168: 169: 170: 171: 172: 173:
174: public static function indent($s, $level = 1, $chars = "\t")
175: {
176: if ($level >= 1) {
177: $s = preg_replace_callback('#<(textarea|pre).*?</\\1#si', create_function('$m', 'return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");'), $s);
178: $s = Nette\String::indent($s, $level, $chars);
179: $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
180: }
181: return $s;
182: }
183:
184:
185:
186: 187: 188: 189: 190: 191:
192: public static function date($time, $format = "%x")
193: {
194: if ($time == NULL) {
195: return NULL;
196: }
197:
198: $time = Nette\DateTime::from($time);
199: return strpos($format, '%') === FALSE
200: ? $time->format($format)
201: : strftime($format, $time->format('U'));
202: }
203:
204:
205:
206: 207: 208: 209: 210: 211:
212: public static function bytes($bytes, $precision = 2)
213: {
214: $bytes = round($bytes);
215: $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
216: foreach ($units as $unit) {
217: if (abs($bytes) < 1024 || $unit === end($units)) break;
218: $bytes = $bytes / 1024;
219: }
220: return round($bytes, $precision) . ' ' . $unit;
221: }
222:
223:
224:
225: 226: 227: 228: 229:
230: public static function length($var)
231: {
232: return is_string($var) ? iconv_strlen($var, 'UTF-8') : count($var);
233: }
234:
235:
236:
237: 238: 239: 240: 241: 242: 243:
244: public static function replace($subject, $search, $replacement = '')
245: {
246: return str_replace($search, $replacement, $subject);
247: }
248:
249:
250:
251: 252: 253: 254: 255: 256: 257:
258: public static function replaceRe($subject, $pattern, $replacement = '')
259: {
260: return preg_replace($pattern, $replacement, $subject);
261: }
262:
263:
264:
265: 266: 267: 268: 269:
270: public static function null($value)
271: {
272: return '';
273: }
274:
275: }
276: