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 NFileTemplate extends NTemplate implements IFileTemplate
22: {
23: /** @var string */
24: private $file;
25:
26:
27: /**
28: * Constructor.
29: * @param string template file path
30: */
31: public function __construct($file = NULL)
32: {
33: if ($file !== NULL) {
34: $this->setFile($file);
35: }
36: }
37:
38:
39: /**
40: * Sets the path to the template file.
41: * @param string template file path
42: * @return self
43: */
44: public function setFile($file)
45: {
46: $this->file = realpath($file);
47: if (!$this->file) {
48: throw new FileNotFoundException("Missing template file '$file'.");
49: }
50: return $this;
51: }
52:
53:
54: /**
55: * Returns the path to the template file.
56: * @return string template file path
57: */
58: public function getFile()
59: {
60: return $this->file;
61: }
62:
63:
64: /**
65: * Returns template source code.
66: * @return string
67: */
68: public function getSource()
69: {
70: return file_get_contents($this->file);
71: }
72:
73:
74: /********************* rendering ****************d*g**/
75:
76:
77: /**
78: * Renders template to output.
79: * @return void
80: */
81: public function render()
82: {
83: if ($this->file == NULL) { // intentionally ==
84: throw new InvalidStateException("Template file name was not specified.");
85: }
86:
87: $cache = new NCache($storage = $this->getCacheStorage(), 'Nette.FileTemplate');
88: if ($storage instanceof NPhpFileStorage) {
89: $storage->hint = str_replace(dirname(dirname($this->file)), '', $this->file);
90: }
91: $cached = $compiled = $cache->load($this->file);
92:
93: if ($compiled === NULL) {
94: try {
95: $compiled = "<?php\n\n// source file: $this->file\n\n?>" . $this->compile();
96:
97: } catch (NTemplateException $e) {
98: $e->setSourceFile($this->file);
99: throw $e;
100: }
101:
102: $cache->save($this->file, $compiled, array(
103: NCache::FILES => $this->file,
104: NCache::CONSTS => 'NFramework::REVISION',
105: ));
106: $cached = $cache->load($this->file);
107: }
108:
109: if ($cached !== NULL && $storage instanceof NPhpFileStorage) {
110: NLimitedScope::load($cached['file'], $this->getParameters());
111: } else {
112: NLimitedScope::evaluate($compiled, $this->getParameters());
113: }
114: }
115:
116: }
117: