Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • BaseTemplate
  • CachingHelper
  • CurlyBracketsMacros
  • LatteFilter
  • LatteMacros
  • SnippetHelper
  • Template
  • TemplateCacheStorage
  • TemplateFilters
  • TemplateHelpers

Interfaces

  • IFileTemplate
  • ITemplate
  • Overview
  • Namespace
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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\Templates;
 13: 
 14: use Nette,
 15:     Nette\Forms\Form,
 16:     Nette\Web\Html;
 17: 
 18: 
 19: 
 20: /**
 21:  * Standard template run-time helpers shipped with Nette Framework (https://nette.org)
 22:  *
 23:  * @author     David Grudl
 24:  */
 25: final class TemplateHelpers
 26: {
 27: 
 28:     /**
 29:      * Static class - cannot be instantiated.
 30:      */
 31:     final public function __construct()
 32:     {
 33:         throw new \LogicException("Cannot instantiate static class " . get_class($this));
 34:     }
 35: 
 36: 
 37: 
 38:     /**
 39:      * Try to load the requested helper.
 40:      * @param  string  helper name
 41:      * @return callback
 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:      * Escapes string for use inside HTML template.
 59:      * @param  mixed  UTF-8 encoding or 8-bit
 60:      * @return string
 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:      * Escapes string for use inside HTML comments.
 74:      * @param  mixed  UTF-8 encoding or 8-bit
 75:      * @return string
 76:      */
 77:     public static function escapeHtmlComment($s)
 78:     {
 79:         // -- has special meaning in different browsers
 80:         return str_replace('--', '--><!-- ', $s); // HTML tags have no meaning inside comments
 81:     }
 82: 
 83: 
 84: 
 85:     /**
 86:      * Escapes string for use inside XML 1.0 template.
 87:      * @param  string UTF-8 encoding or 8-bit
 88:      * @return string
 89:      */
 90:     public static function escapeXML($s)
 91:     {
 92:         // XML 1.0: \x09 \x0A \x0D and C1 allowed directly, C0 forbidden
 93:         // XML 1.1: \x00 forbidden directly and as a character reference, \x09 \x0A \x0D \x85 allowed directly, C0, C1 and \x7F allowed as character references
 94:         return htmlSpecialChars(preg_replace('#[\x00-\x08\x0B\x0C\x0E-\x1F]+#', '', $s), ENT_QUOTES);
 95:     }
 96: 
 97: 
 98: 
 99:     /**
100:      * Escapes string for use inside CSS template.
101:      * @param  string UTF-8 encoding or 8-bit
102:      * @return string
103:      */
104:     public static function escapeCss($s)
105:     {
106:         // http://www.w3.org/TR/2006/WD-CSS21-20060411/syndata.html#q6
107:         return addcslashes($s, "\x00..\x1F!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~");
108:     }
109: 
110: 
111: 
112:     /**
113:      * Escapes string for use inside HTML style attribute.
114:      * @param  string UTF-8 encoding or 8-bit
115:      * @return string
116:      */
117:     public static function escapeHtmlCss($s)
118:     {
119:         return htmlSpecialChars(self::escapeCss($s), ENT_QUOTES);
120:     }
121: 
122: 
123: 
124:     /**
125:      * Escapes string for use inside JavaScript template.
126:      * @param  mixed  UTF-8 encoding
127:      * @return string
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:      * Escapes string for use inside HTML JavaScript attribute.
141:      * @param  mixed  UTF-8 encoding
142:      * @return string
143:      */
144:     public static function escapeHtmlJs($s)
145:     {
146:         return htmlSpecialChars(self::escapeJs($s), ENT_QUOTES);
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 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:      * Indents the HTML content from the left.
169:      * @param  string UTF-8 encoding or 8-bit
170:      * @param  int
171:      * @param  string
172:      * @return string
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:      * Date/time formatting.
188:      * @param  string|int|DateTime
189:      * @param  string
190:      * @return string
191:      */
192:     public static function date($time, $format = "%x")
193:     {
194:         if ($time == NULL) { // intentionally ==
195:             return NULL;
196:         }
197: 
198:         $time = Nette\DateTime::from($time);
199:         return strpos($format, '%') === FALSE
200:             ? $time->format($format) // formats using date()
201:             : strftime($format, $time->format('U')); // formats according to locales
202:     }
203: 
204: 
205: 
206:     /**
207:      * Converts to human readable file size.
208:      * @param  int
209:      * @param  int
210:      * @return string
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:      * Returns array of string length.
227:      * @param  mixed
228:      * @return int
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:      * Performs a search and replace.
239:      * @param  string
240:      * @param  string
241:      * @param  string
242:      * @return string
243:      */
244:     public static function replace($subject, $search, $replacement = '')
245:     {
246:         return str_replace($search, $replacement, $subject);
247:     }
248: 
249: 
250: 
251:     /**
252:      * Performs a regular expression search and replace.
253:      * @param  string
254:      * @param  string
255:      * @param  string
256:      * @return string
257:      */
258:     public static function replaceRe($subject, $pattern, $replacement = '')
259:     {
260:         return preg_replace($pattern, $replacement, $subject);
261:     }
262: 
263: 
264: 
265:     /**
266:      * /dev/null.
267:      * @param  mixed
268:      * @return string
269:      */
270:     public static function null($value)
271:     {
272:         return '';
273:     }
274: 
275: }
276: 
Nette Framework 0.9.7 API documentation generated by ApiGen 2.3.0