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