1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Bridges\ApplicationLatte;
9:
10: use Latte;
11: use Nette;
12:
13:
14: 15: 16:
17: class Template implements Nette\Application\UI\ITemplate
18: {
19: use Nette\SmartObject;
20:
21:
22: private $latte;
23:
24:
25: private $file;
26:
27:
28: private $params = [];
29:
30:
31: public function __construct(Latte\Engine $latte)
32: {
33: $this->latte = $latte;
34: }
35:
36:
37: 38: 39:
40: public function getLatte()
41: {
42: return $this->latte;
43: }
44:
45:
46: 47: 48: 49:
50: public function render($file = null, array $params = [])
51: {
52: $this->latte->render($file ?: $this->file, $params + $this->params);
53: }
54:
55:
56: 57: 58: 59:
60: public function renderToString($file = null, array $params = [])
61: {
62: return $this->latte->renderToString($file ?: $this->file, $params + $this->params);
63: }
64:
65:
66: 67: 68: 69: 70:
71: public function __toString()
72: {
73: try {
74: return $this->latte->renderToString($this->file, $this->params);
75: } catch (\Exception $e) {
76: } catch (\Throwable $e) {
77: }
78: if (isset($e)) {
79: if (func_num_args()) {
80: throw $e;
81: }
82: trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
83: }
84: }
85:
86:
87:
88:
89:
90: 91: 92: 93: 94: 95:
96: public function addFilter($name, $callback)
97: {
98: $this->latte->addFilter($name, $callback);
99: return $this;
100: }
101:
102:
103: 104: 105: 106:
107: public function registerHelper($name, $callback)
108: {
109: trigger_error(__METHOD__ . '() is deprecated, use getLatte()->addFilter().', E_USER_DEPRECATED);
110: return $this->latte->addFilter($name, $callback);
111: }
112:
113:
114: 115: 116: 117:
118: public function setTranslator(Nette\Localization\ITranslator $translator = null)
119: {
120: $this->latte->addFilter('translate', $translator === null ? null : function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator) {
121: return $translator->translate(...$args);
122: });
123: return $this;
124: }
125:
126:
127:
128:
129:
130: 131: 132: 133: 134:
135: public function setFile($file)
136: {
137: $this->file = $file;
138: return $this;
139: }
140:
141:
142: 143: 144:
145: public function getFile()
146: {
147: return $this->file;
148: }
149:
150:
151: 152: 153: 154:
155: public function add($name, $value)
156: {
157: if (array_key_exists($name, $this->params)) {
158: throw new Nette\InvalidStateException("The variable '$name' already exists.");
159: }
160: $this->params[$name] = $value;
161: return $this;
162: }
163:
164:
165: 166: 167: 168: 169:
170: public function setParameters(array $params)
171: {
172: $this->params = $params + $this->params;
173: return $this;
174: }
175:
176:
177: 178: 179: 180:
181: public function getParameters()
182: {
183: return $this->params;
184: }
185:
186:
187: 188: 189:
190: public function __call($name, $args)
191: {
192: trigger_error('Invoking filters on Template object is deprecated, use getLatte()->invokeFilter().', E_USER_DEPRECATED);
193: return $this->latte->invokeFilter($name, $args);
194: }
195:
196:
197: 198: 199: 200:
201: public function __set($name, $value)
202: {
203: $this->params[$name] = $value;
204: }
205:
206:
207: 208: 209: 210:
211: public function &__get($name)
212: {
213: if (!array_key_exists($name, $this->params)) {
214: trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
215: }
216:
217: return $this->params[$name];
218: }
219:
220:
221: 222: 223: 224:
225: public function __isset($name)
226: {
227: return isset($this->params[$name]);
228: }
229:
230:
231: 232: 233: 234: 235:
236: public function __unset($name)
237: {
238: unset($this->params[$name]);
239: }
240: }
241: