1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Templating;
9:
10: use Nette;
11: use Nette\Caching;
12: use Latte;
13:
14:
15: 16: 17:
18: class FileTemplate extends Template implements IFileTemplate
19: {
20:
21: private $file;
22:
23:
24: 25: 26: 27:
28: public function __construct($file = NULL)
29: {
30:
31: if ($file !== NULL) {
32: $this->setFile($file);
33: }
34: }
35:
36:
37: 38: 39: 40: 41:
42: public function setFile($file)
43: {
44: $this->file = realpath($file);
45: if (!$this->file) {
46: throw new Nette\FileNotFoundException("Missing template file '$file'.");
47: }
48: return $this;
49: }
50:
51:
52: 53: 54: 55:
56: public function getFile()
57: {
58: return $this->file;
59: }
60:
61:
62: 63: 64: 65:
66: public function getSource()
67: {
68: return file_get_contents($this->file);
69: }
70:
71:
72:
73:
74:
75: 76: 77: 78:
79: public function render()
80: {
81: if ($this->file == NULL) {
82: throw new Nette\InvalidStateException('Template file name was not specified.');
83: }
84:
85: if (!$this->getFilters()) {
86: $this->onPrepareFilters($this);
87: }
88:
89: if ($latte = $this->getLatte()) {
90: return $latte->setLoader(new Latte\Loaders\FileLoader)->render($this->file, $this->getParameters());
91: }
92:
93: $cache = new Caching\Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
94: if ($storage instanceof Caching\Storages\PhpFileStorage) {
95: $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file);
96: }
97: $cached = $compiled = $cache->load($this->file);
98:
99: if ($compiled === NULL) {
100: try {
101: $compiled = "<?php\n\n// source file: $this->file\n\n?>" . $this->compile();
102:
103: } catch (FilterException $e) {
104: throw $e->setSource(file_get_contents($this->file), $e->sourceLine, $this->file);
105: }
106:
107: $cache->save($this->file, $compiled, array(
108: Caching\Cache::FILES => $this->file,
109: Caching\Cache::CONSTS => 'Nette\Framework::REVISION',
110: ));
111: $cached = $cache->load($this->file);
112: }
113:
114: $isFile = $cached !== NULL && $storage instanceof Caching\Storages\PhpFileStorage;
115: self::load($isFile ? $cached['file'] : $compiled, $this->getParameters(), $isFile);
116: }
117:
118: }
119: