1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20:
21: class NTemplate extends NBaseTemplate implements IFileTemplate
22: {
23:
24: public static $cacheExpire = NULL;
25:
26:
27: private static $cacheStorage;
28:
29:
30: private $file;
31:
32:
33:
34: 35: 36: 37:
38: public function __construct($file = NULL)
39: {
40: if ($file !== NULL) {
41: $this->setFile($file);
42: }
43: }
44:
45:
46:
47: 48: 49: 50: 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: 65: 66:
67: public function getFile()
68: {
69: return $this->file;
70: }
71:
72:
73:
74:
75:
76:
77:
78: 79: 80: 81:
82: public function render()
83: {
84: if ($this->file == NULL) {
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:
136:
137:
138:
139: 140: 141: 142: 143:
144: public static function setCacheStorage(ICacheStorage $storage)
145: {
146: self::$cacheStorage = $storage;
147: }
148:
149:
150:
151: 152: 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: