Namespaces

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

Classes

  • FileTemplate
  • Helpers
  • Template

Interfaces

  • IFileTemplate
  • ITemplate

Exceptions

  • FilterException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Templating;
  9: 
 10: use Nette,
 11:     Nette\Caching,
 12:     Nette\Utils\Callback;
 13: 
 14: 
 15: /**
 16:  * Template.
 17:  *
 18:  * @author     David Grudl
 19:  */
 20: class Template extends Nette\Object implements ITemplate
 21: {
 22:     /** @var array of function(Template $sender); Occurs before a template is compiled - implement to customize the filters */
 23:     public $onPrepareFilters = array();
 24: 
 25:     /** @var string */
 26:     private $source;
 27: 
 28:     /** @var array */
 29:     private $params = array();
 30: 
 31:     /** @var array compile-time filters */
 32:     private $filters = array();
 33: 
 34:     /** @var array run-time helpers */
 35:     private $helpers = array();
 36: 
 37:     /** @var array */
 38:     private $helperLoaders = array();
 39: 
 40:     /** @var Nette\Caching\IStorage */
 41:     private $cacheStorage;
 42: 
 43: 
 44:     /**
 45:      * Sets template source code.
 46:      * @param  string
 47:      * @return self
 48:      */
 49:     public function setSource($source)
 50:     {
 51:         $this->source = $source;
 52:         return $this;
 53:     }
 54: 
 55: 
 56:     /**
 57:      * Returns template source code.
 58:      * @return source
 59:      */
 60:     public function getSource()
 61:     {
 62:         return $this->source;
 63:     }
 64: 
 65: 
 66:     /********************* rendering ****************d*g**/
 67: 
 68: 
 69:     /**
 70:      * Renders template to output.
 71:      * @return void
 72:      */
 73:     public function render()
 74:     {
 75:         $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.Template');
 76:         $cached = $compiled = $cache->load($this->source);
 77: 
 78:         if ($compiled === NULL) {
 79:             $compiled = $this->compile();
 80:             $cache->save($this->source, $compiled, array(Caching\Cache::CONSTS => 'Nette\Framework::REVISION'));
 81:             $cached = $cache->load($this->source);
 82:         }
 83: 
 84:         if ($cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage) {
 85:             Nette\Utils\LimitedScope::load($cached['file'], $this->getParameters());
 86:         } else {
 87:             Nette\Utils\LimitedScope::evaluate($compiled, $this->getParameters());
 88:         }
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Renders template to file.
 94:      * @param  string
 95:      * @return void
 96:      */
 97:     public function save($file)
 98:     {
 99:         if (file_put_contents($file, $this->__toString(TRUE)) === FALSE) {
100:             throw new Nette\IOException("Unable to save file '$file'.");
101:         }
102:     }
103: 
104: 
105:     /**
106:      * Renders template to string.
107:      * @param  can throw exceptions? (hidden parameter)
108:      * @return string
109:      */
110:     public function __toString()
111:     {
112:         ob_start();
113:         try {
114:             $this->render();
115:             return ob_get_clean();
116: 
117:         } catch (\Exception $e) {
118:             ob_end_clean();
119:             if (func_num_args()) {
120:                 throw $e;
121:             }
122:             trigger_error("Exception in " . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
123:         }
124:     }
125: 
126: 
127:     /**
128:      * Applies filters on template content.
129:      * @return string
130:      */
131:     public function compile()
132:     {
133:         if (!$this->filters) {
134:             $this->onPrepareFilters($this);
135:         }
136: 
137:         $code = $this->getSource();
138:         foreach ($this->filters as $filter) {
139:             $code = self::extractPhp($code, $blocks);
140:             $code = call_user_func($filter, $code);
141:             $code = strtr($code, $blocks); // put PHP code back
142:         }
143: 
144:         return Helpers::optimizePhp($code);
145:     }
146: 
147: 
148:     /********************* template filters & helpers ****************d*g**/
149: 
150: 
151:     /**
152:      * Registers callback as template compile-time filter.
153:      * @param  callable
154:      * @return self
155:      */
156:     public function registerFilter($callback)
157:     {
158:         $this->filters[] = Callback::check($callback);
159:         return $this;
160:     }
161: 
162: 
163:     /**
164:      * Returns all registered compile-time filters.
165:      * @return array
166:      */
167:     public function getFilters()
168:     {
169:         return $this->filters;
170:     }
171: 
172: 
173:     /**
174:      * Registers callback as template run-time helper.
175:      * @param  string
176:      * @param  callable
177:      * @return self
178:      */
179:     public function registerHelper($name, $callback)
180:     {
181:         $this->helpers[strtolower($name)] = $callback;
182:         return $this;
183:     }
184: 
185: 
186:     /**
187:      * Registers callback as template run-time helpers loader.
188:      * @param  callable
189:      * @return self
190:      */
191:     public function registerHelperLoader($callback)
192:     {
193:         array_unshift($this->helperLoaders, $callback);
194:         return $this;
195:     }
196: 
197: 
198:     /**
199:      * Returns all registered run-time helpers.
200:      * @return array
201:      */
202:     public function getHelpers()
203:     {
204:         return $this->helpers;
205:     }
206: 
207: 
208:     /**
209:      * Returns all registered template run-time helper loaders.
210:      * @return array
211:      */
212:     public function getHelperLoaders()
213:     {
214:         return $this->helperLoaders;
215:     }
216: 
217: 
218:     /**
219:      * Call a template run-time helper. Do not call directly.
220:      * @param  string  helper name
221:      * @param  array   arguments
222:      * @return mixed
223:      */
224:     public function __call($name, $args)
225:     {
226:         $lname = strtolower($name);
227:         if (!isset($this->helpers[$lname])) {
228:             foreach ($this->helperLoaders as $loader) {
229:                 $helper = Callback::invoke($loader, $lname);
230:                 if ($helper) {
231:                     $this->registerHelper($lname, $helper);
232:                     return Callback::invokeArgs($this->helpers[$lname], $args);
233:                 }
234:             }
235:             return parent::__call($name, $args);
236:         }
237: 
238:         return Callback::invokeArgs($this->helpers[$lname], $args);
239:     }
240: 
241: 
242:     /**
243:      * Sets translate adapter.
244:      * @return self
245:      */
246:     public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
247:     {
248:         $this->registerHelper('translate', $translator === NULL ? NULL : array($translator, 'translate'));
249:         return $this;
250:     }
251: 
252: 
253:     /********************* template parameters ****************d*g**/
254: 
255: 
256:     /**
257:      * Adds new template parameter.
258:      * @return self
259:      */
260:     public function add($name, $value)
261:     {
262:         if (array_key_exists($name, $this->params)) {
263:             throw new Nette\InvalidStateException("The variable '$name' already exists.");
264:         }
265: 
266:         $this->params[$name] = $value;
267:         return $this;
268:     }
269: 
270: 
271:     /**
272:      * Sets all parameters.
273:      * @param  array
274:      * @return self
275:      */
276:     public function setParameters(array $params)
277:     {
278:         $this->params = $params + $this->params;
279:         return $this;
280:     }
281: 
282: 
283:     /**
284:      * Returns array of all parameters.
285:      * @return array
286:      */
287:     public function getParameters()
288:     {
289:         $this->params['template'] = $this;
290:         return $this->params;
291:     }
292: 
293: 
294:     /**
295:      * Sets a template parameter. Do not call directly.
296:      * @return void
297:      */
298:     public function __set($name, $value)
299:     {
300:         $this->params[$name] = $value;
301:     }
302: 
303: 
304:     /**
305:      * Returns a template parameter. Do not call directly.
306:      * @return mixed  value
307:      */
308:     public function &__get($name)
309:     {
310:         if (!array_key_exists($name, $this->params)) {
311:             trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
312:         }
313: 
314:         return $this->params[$name];
315:     }
316: 
317: 
318:     /**
319:      * Determines whether parameter is defined. Do not call directly.
320:      * @return bool
321:      */
322:     public function __isset($name)
323:     {
324:         return isset($this->params[$name]);
325:     }
326: 
327: 
328:     /**
329:      * Removes a template parameter. Do not call directly.
330:      * @param  string    name
331:      * @return void
332:      */
333:     public function __unset($name)
334:     {
335:         unset($this->params[$name]);
336:     }
337: 
338: 
339:     /********************* caching ****************d*g**/
340: 
341: 
342:     /**
343:      * Set cache storage.
344:      * @return self
345:      */
346:     public function setCacheStorage(Caching\IStorage $storage)
347:     {
348:         $this->cacheStorage = $storage;
349:         return $this;
350:     }
351: 
352: 
353:     /**
354:      * @return Nette\Caching\IStorage
355:      */
356:     public function getCacheStorage()
357:     {
358:         if ($this->cacheStorage === NULL) {
359:             return new Caching\Storages\DevNullStorage;
360:         }
361:         return $this->cacheStorage;
362:     }
363: 
364: 
365:     /********************* tools ****************d*g**/
366: 
367: 
368:     /**
369:      * Extracts all blocks of PHP code.
370:      * @param  string
371:      * @param  array
372:      * @return string
373:      */
374:     private static function extractPhp($source, & $blocks)
375:     {
376:         $res = '';
377:         $blocks = array();
378:         $tokens = token_get_all($source);
379:         foreach ($tokens as $n => $token) {
380:             if (is_array($token)) {
381:                 if ($token[0] === T_INLINE_HTML) {
382:                     $res .= $token[1];
383:                     continue;
384: 
385:                 } elseif ($token[0] === T_CLOSE_TAG) {
386:                     if ($php !== $res) { // not <?xml
387:                         $res .= str_repeat("\n", substr_count($php, "\n"));
388:                     }
389:                     $res .= $token[1];
390:                     continue;
391: 
392:                 } elseif ($token[0] === T_OPEN_TAG && $token[1] === '<?' && isset($tokens[$n+1][1]) && $tokens[$n+1][1] === 'xml') {
393:                     $php = & $res;
394:                     $token[1] = '<<?php ?>?';
395: 
396:                 } elseif ($token[0] === T_OPEN_TAG || $token[0] === T_OPEN_TAG_WITH_ECHO) {
397:                     $res .= $id = "<?php \x01@php:p" . count($blocks) . "@\x02";
398:                     $php = & $blocks[$id];
399:                 }
400:                 $php .= $token[1];
401: 
402:             } else {
403:                 $php .= $token;
404:             }
405:         }
406:         return $res;
407:     }
408: 
409: }
410: 
Nette Framework 2.1.8 API API documentation generated by ApiGen 2.8.0