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