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
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Loader
  • Template
  • TemplateFactory
  • UIMacros

Interfaces

  • ILatteFactory
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  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 string.
 58:      * @param  can throw exceptions? (hidden parameter)
 59:      * @return string
 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:     /********************* template filters & helpers ****************d*g**/
 78: 
 79: 
 80:     /**
 81:      * Registers run-time filter.
 82:      * @param  string|null
 83:      * @param  callable
 84:      * @return static
 85:      */
 86:     public function addFilter($name, $callback)
 87:     {
 88:         $this->latte->addFilter($name, $callback);
 89:         return $this;
 90:     }
 91: 
 92: 
 93:     /**
 94:      * Alias for addFilter()
 95:      * @deprecated
 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:      * Sets translate adapter.
106:      * @return static
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:     /********************* template parameters ****************d*g**/
118: 
119: 
120:     /**
121:      * Sets the path to the template file.
122:      * @param  string
123:      * @return static
124:      */
125:     public function setFile($file)
126:     {
127:         $this->file = $file;
128:         return $this;
129:     }
130: 
131: 
132:     /**
133:      * @return string|null
134:      */
135:     public function getFile()
136:     {
137:         return $this->file;
138:     }
139: 
140: 
141:     /**
142:      * Adds new template parameter.
143:      * @return static
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:      * Sets all parameters.
157:      * @param  array
158:      * @return static
159:      */
160:     public function setParameters(array $params)
161:     {
162:         $this->params = $params + $this->params;
163:         return $this;
164:     }
165: 
166: 
167:     /**
168:      * Returns array of all parameters.
169:      * @return array
170:      */
171:     public function getParameters()
172:     {
173:         return $this->params;
174:     }
175: 
176: 
177:     /**
178:      * @deprecated
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:      * Sets a template parameter. Do not call directly.
189:      * @return void
190:      */
191:     public function __set($name, $value)
192:     {
193:         $this->params[$name] = $value;
194:     }
195: 
196: 
197:     /**
198:      * Returns a template parameter. Do not call directly.
199:      * @return mixed  value
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:      * Determines whether parameter is defined. Do not call directly.
213:      * @return bool
214:      */
215:     public function __isset($name)
216:     {
217:         return isset($this->params[$name]);
218:     }
219: 
220: 
221:     /**
222:      * Removes a template parameter. Do not call directly.
223:      * @param  string    name
224:      * @return void
225:      */
226:     public function __unset($name)
227:     {
228:         unset($this->params[$name]);
229:     }
230: }
231: 
Nette 2.4-20170829 API API documentation generated by ApiGen 2.8.0