Packages

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

Classes

  • NBaseTemplate
  • NCachingHelper
  • NCurlyBracketsMacros
  • NLatteFilter
  • NLatteMacros
  • NSnippetHelper
  • NTemplate
  • NTemplateCacheStorage
  • NTemplateFilters
  • NTemplateHelpers

Interfaces

  • IFileTemplate
  • ITemplate
  • Overview
  • Package
  • 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:  * @package Nette\Templates
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Template stored in file.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Templates
 20:  */
 21: class NTemplate extends NBaseTemplate implements IFileTemplate
 22: {
 23:     /** @var int */
 24:     public static $cacheExpire = NULL;
 25: 
 26:     /** @var ICacheStorage */
 27:     private static $cacheStorage;
 28: 
 29:     /** @var string */
 30:     private $file;
 31: 
 32: 
 33: 
 34:     /**
 35:      * Constructor.
 36:      * @param  string  template file path
 37:      */
 38:     public function __construct($file = NULL)
 39:     {
 40:         if ($file !== NULL) {
 41:             $this->setFile($file);
 42:         }
 43:     }
 44: 
 45: 
 46: 
 47:     /**
 48:      * Sets the path to the template file.
 49:      * @param  string  template file path
 50:      * @return NTemplate  provides a fluent interface
 51:      */
 52:     public function setFile($file)
 53:     {
 54:         if (!is_file($file)) {
 55:             throw new FileNotFoundException("Missing template file '$file'.");
 56:         }
 57:         $this->file = $file;
 58:         return $this;
 59:     }
 60: 
 61: 
 62: 
 63:     /**
 64:      * Returns the path to the template file.
 65:      * @return string  template file path
 66:      */
 67:     public function getFile()
 68:     {
 69:         return $this->file;
 70:     }
 71: 
 72: 
 73: 
 74:     /********************* rendering ****************d*g**/
 75: 
 76: 
 77: 
 78:     /**
 79:      * Renders template to output.
 80:      * @return void
 81:      */
 82:     public function render()
 83:     {
 84:         if ($this->file == NULL) { // intentionally ==
 85:             throw new InvalidStateException("Template file name was not specified.");
 86:         }
 87: 
 88:         $this->__set('template', $this);
 89: 
 90:         $cache = new NCache($this->getCacheStorage(), 'Nette.Template');
 91:         $key = md5($this->file) . '.' . basename($this->file);
 92:         $cached = $content = $cache[$key];
 93: 
 94:         if ($content === NULL) {
 95:             if (!$this->getFilters()) {
 96:                 $this->onPrepareFilters($this);
 97:             }
 98: 
 99:             if (!$this->getFilters()) {
100:                 NLimitedScope::load($this->file, $this->getParams());
101:                 return;
102:             }
103: 
104:             try {
105:                 $shortName = $this->file;
106:                 $shortName = str_replace(NEnvironment::getVariable('appDir'), "\xE2\x80\xA6", $shortName);
107:             } catch (Exception $foo) {
108:             }
109: 
110:             $content = $this->compile(file_get_contents($this->file), "file $shortName");
111:             $cache->save(
112:                 $key,
113:                 $content,
114:                 array(
115:                     NCache::FILES => $this->file,
116:                     NCache::EXPIRE => self::$cacheExpire,
117:                 )
118:             );
119:             $cache->release();
120:             $cached = $cache[$key];
121:         }
122: 
123:         if ($cached !== NULL && self::$cacheStorage instanceof NTemplateCacheStorage) {
124:             NLimitedScope::load($cached['file'], $this->getParams());
125:             flock($cached['handle'], LOCK_UN);
126:             fclose($cached['handle']);
127: 
128:         } else {
129:             NLimitedScope::evaluate($content, $this->getParams());
130:         }
131:     }
132: 
133: 
134: 
135:     /********************* caching ****************d*g**/
136: 
137: 
138: 
139:     /**
140:      * Set cache storage.
141:      * @param  NCache
142:      * @return void
143:      */
144:     public static function setCacheStorage(ICacheStorage $storage)
145:     {
146:         self::$cacheStorage = $storage;
147:     }
148: 
149: 
150: 
151:     /**
152:      * @return ICacheStorage
153:      */
154:     public static function getCacheStorage()
155:     {
156:         if (self::$cacheStorage === NULL) {
157:             self::$cacheStorage = new NTemplateCacheStorage(NEnvironment::getVariable('tempDir'));
158:         }
159:         return self::$cacheStorage;
160:     }
161: 
162: }
163: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0