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