Packages

  • 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
  • Template
  • TemplateHelpers

Interfaces

  • IFileTemplate
  • ITemplate

Exceptions

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