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