Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • FileTemplate
  • Helpers
  • Template

Interfaces

  • IFileTemplate
  • ITemplate

Exceptions

  • FilterException
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Templating;
 13: 
 14: use Nette,
 15:     Nette\Utils\Strings,
 16:     Nette\Forms\Form,
 17:     Nette\Utils\Html;
 18: 
 19: 
 20: 
 21: /**
 22:  * Template helpers.
 23:  *
 24:  * @author     David Grudl
 25:  */
 26: final class Helpers
 27: {
 28:     private static $helpers = array(
 29:         'normalize' => 'Nette\Utils\Strings::normalize',
 30:         'toascii' => 'Nette\Utils\Strings::toAscii',
 31:         'webalize' => 'Nette\Utils\Strings::webalize',
 32:         'truncate' => 'Nette\Utils\Strings::truncate',
 33:         'lower' => 'Nette\Utils\Strings::lower',
 34:         'upper' => 'Nette\Utils\Strings::upper',
 35:         'firstupper' => 'Nette\Utils\Strings::firstUpper',
 36:         'capitalize' => 'Nette\Utils\Strings::capitalize',
 37:         'trim' => 'Nette\Utils\Strings::trim',
 38:         'padleft' => 'Nette\Utils\Strings::padLeft',
 39:         'padright' => 'Nette\Utils\Strings::padRight',
 40:         'replacere' => 'Nette\Utils\Strings::replace',
 41:         'url' => 'rawurlencode',
 42:         'striptags' => 'strip_tags',
 43:         'nl2br' => 'nl2br',
 44:         'substr' => 'Nette\Utils\Strings::substring',
 45:         'repeat' => 'str_repeat',
 46:         'implode' => 'implode',
 47:         'number' => 'number_format',
 48:     );
 49: 
 50:     /** @var string default date format */
 51:     public static $dateFormat = '%x';
 52: 
 53: 
 54: 
 55:     /**
 56:      * Try to load the requested helper.
 57:      * @param  string  helper name
 58:      * @return callback
 59:      */
 60:     public static function loader($helper)
 61:     {
 62:         if (method_exists(__CLASS__, $helper)) {
 63:             return callback(__CLASS__, $helper);
 64:         } elseif (isset(self::$helpers[$helper])) {
 65:             return self::$helpers[$helper];
 66:         }
 67:     }
 68: 
 69: 
 70: 
 71:     /**
 72:      * Escapes string for use inside HTML template.
 73:      * @param  mixed  UTF-8 encoding
 74:      * @param  int    optional attribute quotes
 75:      * @return string
 76:      */
 77:     public static function escapeHtml($s, $quotes = ENT_QUOTES)
 78:     {
 79:         if (is_object($s) && ($s instanceof ITemplate || $s instanceof Html || $s instanceof Form)) {
 80:             return $s->__toString(TRUE);
 81:         }
 82:         return htmlSpecialChars($s, $quotes);
 83:     }
 84: 
 85: 
 86: 
 87:     /**
 88:      * Escapes string for use inside HTML comments.
 89:      * @param  string  UTF-8 encoding
 90:      * @return string
 91:      */
 92:     public static function escapeHtmlComment($s)
 93:     {
 94:         // -- has special meaning in different browsers
 95:         return str_replace('--', '--><!-- ', $s); // HTML tags have no meaning inside comments
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * Escapes string for use inside XML 1.0 template.
102:      * @param  string UTF-8 encoding
103:      * @return string
104:      */
105:     public static function escapeXML($s)
106:     {
107:         // XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
108:         // XML 1.1: \x00 forbidden directly and as a character reference,
109:         //   \x09 \x0A \x0D \x85 allowed directly, C0, C1 and \x7F allowed as character references
110:         return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
111:     }
112: 
113: 
114: 
115:     /**
116:      * Escapes string for use inside CSS template.
117:      * @param  string UTF-8 encoding
118:      * @return string
119:      */
120:     public static function escapeCss($s)
121:     {
122:         // http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
123:         return addcslashes($s, "\x00..\x1F!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~");
124:     }
125: 
126: 
127: 
128:     /**
129:      * Escapes string for use inside JavaScript template.
130:      * @param  mixed  UTF-8 encoding
131:      * @return string
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\Utils\Json::encode($s));
139:     }
140: 
141: 
142: 
143:     /**
144:      * Escapes string for use inside iCal template.
145:      * @param  mixed  UTF-8 encoding
146:      * @return string
147:      */
148:     public static function escapeICal($s)
149:     {
150:         // http://www.ietf.org/rfc/rfc5545.txt
151:         return addcslashes(preg_replace('#[\x00-\x08\x0B\x0C-\x1F]+#', '', $s), "\";\\,:\n");
152:     }
153: 
154: 
155: 
156:     /**
157:      * Replaces all repeated white spaces with a single space.
158:      * @param  string UTF-8 encoding or 8-bit
159:      * @return string
160:      */
161:     public static function strip($s)
162:     {
163:         return Strings::replace(
164:             $s,
165:             '#(</textarea|</pre|</script|^).*?(?=<textarea|<pre|<script|$)#si',
166:             function($m) {
167:                 return trim(preg_replace("#[ \t\r\n]+#", " ", $m[0]));
168:             });
169:     }
170: 
171: 
172: 
173:     /**
174:      * Indents the HTML content from the left.
175:      * @param  string UTF-8 encoding or 8-bit
176:      * @param  int
177:      * @param  string
178:      * @return string
179:      */
180:     public static function indent($s, $level = 1, $chars = "\t")
181:     {
182:         if ($level >= 1) {
183:             $s = Strings::replace($s, '#<(textarea|pre).*?</\\1#si', function($m) {
184:                 return strtr($m[0], " \t\r\n", "\x1F\x1E\x1D\x1A");
185:             });
186:             $s = Strings::indent($s, $level, $chars);
187:             $s = strtr($s, "\x1F\x1E\x1D\x1A", " \t\r\n");
188:         }
189:         return $s;
190:     }
191: 
192: 
193: 
194:     /**
195:      * Date/time formatting.
196:      * @param  string|int|DateTime
197:      * @param  string
198:      * @return string
199:      */
200:     public static function date($time, $format = NULL)
201:     {
202:         if ($time == NULL) { // intentionally ==
203:             return NULL;
204:         }
205: 
206:         if (!isset($format)) {
207:             $format = self::$dateFormat;
208:         }
209: 
210:         $time = Nette\DateTime::from($time);
211:         return Strings::contains($format, '%')
212:             ? strftime($format, $time->format('U')) // formats according to locales
213:             : $time->format($format); // formats using date()
214:     }
215: 
216: 
217: 
218:     /**
219:      * Converts to human readable file size.
220:      * @param  int
221:      * @param  int
222:      * @return string
223:      */
224:     public static function bytes($bytes, $precision = 2)
225:     {
226:         $bytes = round($bytes);
227:         $units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB');
228:         foreach ($units as $unit) {
229:             if (abs($bytes) < 1024 || $unit === end($units)) {
230:                 break;
231:             }
232:             $bytes = $bytes / 1024;
233:         }
234:         return round($bytes, $precision) . ' ' . $unit;
235:     }
236: 
237: 
238: 
239:     /**
240:      * Returns array of string length.
241:      * @param  mixed
242:      * @return int
243:      */
244:     public static function length($var)
245:     {
246:         return is_string($var) ? Strings::length($var) : count($var);
247:     }
248: 
249: 
250: 
251:     /**
252:      * Performs a search and replace.
253:      * @param  string
254:      * @param  string
255:      * @param  string
256:      * @return string
257:      */
258:     public static function replace($subject, $search, $replacement = '')
259:     {
260:         return str_replace($search, $replacement, $subject);
261:     }
262: 
263: 
264: 
265:     /**
266:      * The data: URI generator.
267:      * @param  string
268:      * @param  string
269:      * @return string
270:      */
271:     public static function dataStream($data, $type = NULL)
272:     {
273:         if ($type === NULL) {
274:             $type = Nette\Utils\MimeTypeDetector::fromString($data, NULL);
275:         }
276:         return 'data:' . ($type ? "$type;" : '') . 'base64,' . base64_encode($data);
277:     }
278: 
279: 
280: 
281:     /**
282:      * /dev/null.
283:      * @param  mixed
284:      * @return string
285:      */
286:     public static function null($value)
287:     {
288:         return '';
289:     }
290: 
291: 
292: 
293:     /********************* Template tools ****************d*g**/
294: 
295: 
296: 
297:     /**
298:      * Removes unnecessary blocks of PHP code.
299:      * @param  string
300:      * @return string
301:      */
302:     public static function optimizePhp($source, $lineLength = 80, $existenceOfThisParameterSolvesDamnBugInPHP535 = NULL)
303:     {
304:         $res = $php = '';
305:         $lastChar = ';';
306:         $tokens = new \ArrayIterator(token_get_all($source));
307:         foreach ($tokens as $key => $token) {
308:             if (is_array($token)) {
309:                 if ($token[0] === T_INLINE_HTML) {
310:                     $lastChar = '';
311:                     $res .= $token[1];
312: 
313:                 } elseif ($token[0] === T_CLOSE_TAG) {
314:                     $next = isset($tokens[$key + 1]) ? $tokens[$key + 1] : NULL;
315:                     if (substr($res, -1) !== '<' && preg_match('#^<\?php\s*$#', $php)) {
316:                         $php = ''; // removes empty (?php ?), but retains ((?php ?)?php
317: 
318:                     } elseif (is_array($next) && $next[0] === T_OPEN_TAG) { // remove ?)(?php
319:                         if (!strspn($lastChar, ';{}:/')) {
320:                             $php .= $lastChar = ';';
321:                         }
322:                         if (substr($next[1], -1) === "\n") {
323:                             $php .= "\n";
324:                         }
325:                         $tokens->next();
326: 
327:                     } elseif ($next) {
328:                         $res .= preg_replace('#;?(\s)*$#', '$1', $php) . $token[1]; // remove last semicolon before ?)
329:                         if (strlen($res) - strrpos($res, "\n") > $lineLength
330:                             && (!is_array($next) || strpos($next[1], "\n") === FALSE)
331:                         ) {
332:                             $res .= "\n";
333:                         }
334:                         $php = '';
335: 
336:                     } else { // remove last ?)
337:                         if (!strspn($lastChar, '};')) {
338:                             $php .= ';';
339:                         }
340:                     }
341: 
342:                 } elseif ($token[0] === T_ELSE || $token[0] === T_ELSEIF) {
343:                     if ($tokens[$key + 1] === ':' && $lastChar === '}') {
344:                         $php .= ';'; // semicolon needed in if(): ... if() ... else:
345:                     }
346:                     $lastChar = '';
347:                     $php .= $token[1];
348: 
349:                 } else {
350:                     if (!in_array($token[0], array(T_WHITESPACE, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG))) {
351:                         $lastChar = '';
352:                     }
353:                     $php .= $token[1];
354:                 }
355:             } else {
356:                 $php .= $lastChar = $token;
357:             }
358:         }
359:         return $res . $php;
360:     }
361: 
362: }
363: 
Nette Framework 2.0.0 API API documentation generated by ApiGen 2.7.0