1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://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\Templating
11: */
12:
13:
14:
15: /**
16: * Template stored in file.
17: *
18: * @author David Grudl
19: * @package Nette\Templating
20: */
21: class FileTemplate extends Template implements IFileTemplate
22: {
23: /** @var string */
24: private $file;
25:
26:
27:
28: /**
29: * Constructor.
30: * @param string template file path
31: */
32: public function __construct($file = NULL)
33: {
34: if ($file !== NULL) {
35: $this->setFile($file);
36: }
37: }
38:
39:
40:
41: /**
42: * Sets the path to the template file.
43: * @param string template file path
44: * @return FileTemplate provides a fluent interface
45: */
46: public function setFile($file)
47: {
48: $this->file = realpath($file);
49: if (!$this->file) {
50: throw new FileNotFoundException("Missing template file '$file'.");
51: }
52: return $this;
53: }
54:
55:
56:
57: /**
58: * Returns the path to the template file.
59: * @return string template file path
60: */
61: public function getFile()
62: {
63: return $this->file;
64: }
65:
66:
67:
68: /**
69: * Returns template source code.
70: * @return string
71: */
72: public function getSource()
73: {
74: return file_get_contents($this->file);
75: }
76:
77:
78:
79: /********************* rendering ****************d*g**/
80:
81:
82:
83: /**
84: * Renders template to output.
85: * @return void
86: */
87: public function render()
88: {
89: if ($this->file == NULL) { // intentionally ==
90: throw new InvalidStateException("Template file name was not specified.");
91: }
92:
93: $cache = new Cache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
94: if ($storage instanceof 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 (TemplateException $e) {
104: $e->setSourceFile($this->file);
105: throw $e;
106: }
107:
108: $cache->save($this->file, $compiled, array(
109: Cache::FILES => $this->file,
110: Cache::CONSTS => 'Framework::REVISION',
111: ));
112: $cached = $cache->load($this->file);
113: }
114:
115: if ($cached !== NULL && $storage instanceof PhpFileStorage) {
116: LimitedScope::load($cached['file'], $this->getParameters());
117: } else {
118: LimitedScope::evaluate($compiled, $this->getParameters());
119: }
120: }
121:
122: }
123: