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:
61: public function __toString()
62: {
63: try {
64: return $this->latte->renderToString($this->file, $this->params);
65: } catch (\Exception $e) {
66: } catch (\Throwable $e) {
67: }
68: if (isset($e)) {
69: if (func_num_args()) {
70: throw $e;
71: }
72: trigger_error('Exception in ' . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
73: }
74: }
75:
76:
77:
78:
79:
80: 81: 82: 83: 84: 85:
86: public function addFilter($name, $callback)
87: {
88: $this->latte->addFilter($name, $callback);
89: return $this;
90: }
91:
92:
93: 94: 95: 96:
97: public function registerHelper($name, $callback)
98: {
99: trigger_error(__METHOD__ . '() is deprecated, use getLatte()->addFilter().', E_USER_DEPRECATED);
100: return $this->latte->addFilter($name, $callback);
101: }
102:
103:
104: 105: 106: 107:
108: public function setTranslator(Nette\Localization\ITranslator $translator = null)
109: {
110: $this->latte->addFilter('translate', $translator === null ? null : function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator) {
111: return $translator->translate(...$args);
112: });
113: return $this;
114: }
115:
116:
117:
118:
119:
120: 121: 122: 123: 124:
125: public function setFile($file)
126: {
127: $this->file = $file;
128: return $this;
129: }
130:
131:
132: 133: 134:
135: public function getFile()
136: {
137: return $this->file;
138: }
139:
140:
141: 142: 143: 144:
145: public function add($name, $value)
146: {
147: if (array_key_exists($name, $this->params)) {
148: throw new Nette\InvalidStateException("The variable '$name' already exists.");
149: }
150: $this->params[$name] = $value;
151: return $this;
152: }
153:
154:
155: 156: 157: 158: 159:
160: public function setParameters(array $params)
161: {
162: $this->params = $params + $this->params;
163: return $this;
164: }
165:
166:
167: 168: 169: 170:
171: public function getParameters()
172: {
173: return $this->params;
174: }
175:
176:
177: 178: 179:
180: public function __call($name, $args)
181: {
182: trigger_error('Invoking filters on Template object is deprecated, use getLatte()->invokeFilter().', E_USER_DEPRECATED);
183: return $this->latte->invokeFilter($name, $args);
184: }
185:
186:
187: 188: 189: 190:
191: public function __set($name, $value)
192: {
193: $this->params[$name] = $value;
194: }
195:
196:
197: 198: 199: 200:
201: public function &__get($name)
202: {
203: if (!array_key_exists($name, $this->params)) {
204: trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
205: }
206:
207: return $this->params[$name];
208: }
209:
210:
211: 212: 213: 214:
215: public function __isset($name)
216: {
217: return isset($this->params[$name]);
218: }
219:
220:
221: 222: 223: 224: 225:
226: public function __unset($name)
227: {
228: unset($this->params[$name]);
229: }
230: }
231: