Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Loader
  • SnippetBridge
  • Template
  • TemplateFactory
  • UIMacros
  • UIRuntime

Interfaces

  • ILatteFactory
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Bridges\ApplicationLatte;
  9: 
 10: use Latte;
 11: use Nette;
 12: 
 13: 
 14: /**
 15:  * Latte powered template.
 16:  */
 17: class Template implements Nette\Application\UI\ITemplate
 18: {
 19:     use Nette\SmartObject;
 20: 
 21:     /** @var Latte\Engine */
 22:     private $latte;
 23: 
 24:     /** @var string */
 25:     private $file;
 26: 
 27:     /** @var array */
 28:     private $params = [];
 29: 
 30: 
 31:     public function __construct(Latte\Engine $latte)
 32:     {
 33:         $this->latte = $latte;
 34:     }
 35: 
 36: 
 37:     /**
 38:      * @return Latte\Engine
 39:      */
 40:     public function getLatte()
 41:     {
 42:         return $this->latte;
 43:     }
 44: 
 45: 
 46:     /**
 47:      * Renders template to output.
 48:      * @return void
 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:      * Renders template to output.
 58:      * @return string
 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:      * Renders template to string.
 68:      * @param  can throw exceptions? (hidden parameter)
 69:      * @return string
 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:     /********************* template filters & helpers ****************d*g**/
 88: 
 89: 
 90:     /**
 91:      * Registers run-time filter.
 92:      * @param  string|null
 93:      * @param  callable
 94:      * @return static
 95:      */
 96:     public function addFilter($name, $callback)
 97:     {
 98:         $this->latte->addFilter($name, $callback);
 99:         return $this;
100:     }
101: 
102: 
103:     /**
104:      * Alias for addFilter()
105:      * @deprecated
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:      * Sets translate adapter.
116:      * @return static
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:     /********************* template parameters ****************d*g**/
128: 
129: 
130:     /**
131:      * Sets the path to the template file.
132:      * @param  string
133:      * @return static
134:      */
135:     public function setFile($file)
136:     {
137:         $this->file = $file;
138:         return $this;
139:     }
140: 
141: 
142:     /**
143:      * @return string|null
144:      */
145:     public function getFile()
146:     {
147:         return $this->file;
148:     }
149: 
150: 
151:     /**
152:      * Adds new template parameter.
153:      * @return static
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:      * Sets all parameters.
167:      * @param  array
168:      * @return static
169:      */
170:     public function setParameters(array $params)
171:     {
172:         $this->params = $params + $this->params;
173:         return $this;
174:     }
175: 
176: 
177:     /**
178:      * Returns array of all parameters.
179:      * @return array
180:      */
181:     public function getParameters()
182:     {
183:         return $this->params;
184:     }
185: 
186: 
187:     /**
188:      * @deprecated
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:      * Sets a template parameter. Do not call directly.
199:      * @return void
200:      */
201:     public function __set($name, $value)
202:     {
203:         $this->params[$name] = $value;
204:     }
205: 
206: 
207:     /**
208:      * Returns a template parameter. Do not call directly.
209:      * @return mixed  value
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:      * Determines whether parameter is defined. Do not call directly.
223:      * @return bool
224:      */
225:     public function __isset($name)
226:     {
227:         return isset($this->params[$name]);
228:     }
229: 
230: 
231:     /**
232:      * Removes a template parameter. Do not call directly.
233:      * @param  string    name
234:      * @return void
235:      */
236:     public function __unset($name)
237:     {
238:         unset($this->params[$name]);
239:     }
240: }
241: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0