Packages

  • Nette
    • Application
      • Application\Diagnostics
      • Application\Responses
      • Application\Routers
      • Application\UI
    • Caching
      • Caching\Storages
    • ComponentModel
    • Config
    • Database
      • Database\Diagnostics
      • Database\Drivers
      • Database\Reflection
      • Database\Table
    • DI
    • Diagnostics
    • Forms
      • Forms\Controls
      • Forms\Rendering
    • Http
    • Iterators
    • Latte
      • Latte\Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
    • Templating
    • Utils
  • 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, 2011 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:  * Standard template run-time helpers shipped with Nette Framework (http://nette.org)
 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' => 'iconv_substr',
 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 or 8-bit
 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  mixed  UTF-8 encoding or 8-bit
 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 or 8-bit
 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 or 8-bit
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 strpos($format, '%') === FALSE
207:             ? $time->format($format) // formats using date()
208:             : strftime($format, $time->format('U')); // formats according to locales
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: 
Nette Framework 2.0beta1 (for PHP 5.2) API API documentation generated by ApiGen 2.3.0