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

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 Nette;
 11: use Latte;
 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 (\Throwable $e) {
 66:         } catch (\Exception $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 self
 85:      */
 86:     public function addFilter($name, $callback)
 87:     {
 88:         return $this->latte->addFilter($name, $callback);
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Alias for addFilter()
 94:      * @deprecated
 95:      */
 96:     public function registerHelper($name, $callback)
 97:     {
 98:         trigger_error(__METHOD__ . '() is deprecated, use getLatte()->addFilter().', E_USER_DEPRECATED);
 99:         return $this->latte->addFilter($name, $callback);
100:     }
101: 
102: 
103:     /**
104:      * Sets translate adapter.
105:      * @return self
106:      */
107:     public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
108:     {
109:         $this->latte->addFilter('translate', $translator === NULL ? NULL : function (Latte\Runtime\FilterInfo $fi, ...$args) use ($translator) {
110:             return $translator->translate(...$args);
111:         });
112:         return $this;
113:     }
114: 
115: 
116:     /********************* template parameters ****************d*g**/
117: 
118: 
119:     /**
120:      * Sets the path to the template file.
121:      * @param  string
122:      * @return self
123:      */
124:     public function setFile($file)
125:     {
126:         $this->file = $file;
127:         return $this;
128:     }
129: 
130: 
131:     /**
132:      * @return string
133:      */
134:     public function getFile()
135:     {
136:         return $this->file;
137:     }
138: 
139: 
140:     /**
141:      * Adds new template parameter.
142:      * @return self
143:      */
144:     public function add($name, $value)
145:     {
146:         if (array_key_exists($name, $this->params)) {
147:             throw new Nette\InvalidStateException("The variable '$name' already exists.");
148:         }
149:         $this->params[$name] = $value;
150:         return $this;
151:     }
152: 
153: 
154:     /**
155:      * Sets all parameters.
156:      * @param  array
157:      * @return self
158:      */
159:     public function setParameters(array $params)
160:     {
161:         $this->params = $params + $this->params;
162:         return $this;
163:     }
164: 
165: 
166:     /**
167:      * Returns array of all parameters.
168:      * @return array
169:      */
170:     public function getParameters()
171:     {
172:         return $this->params;
173:     }
174: 
175: 
176:     /**
177:      * @deprecated
178:      */
179:     public function __call($name, $args)
180:     {
181:         trigger_error('Invoking filters on Template object is deprecated, use getLatte()->invokeFilter().', E_USER_DEPRECATED);
182:         return $this->latte->invokeFilter($name, $args);
183:     }
184: 
185: 
186:     /**
187:      * Sets a template parameter. Do not call directly.
188:      * @return void
189:      */
190:     public function __set($name, $value)
191:     {
192:         $this->params[$name] = $value;
193:     }
194: 
195: 
196:     /**
197:      * Returns a template parameter. Do not call directly.
198:      * @return mixed  value
199:      */
200:     public function &__get($name)
201:     {
202:         if (!array_key_exists($name, $this->params)) {
203:             trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
204:         }
205: 
206:         return $this->params[$name];
207:     }
208: 
209: 
210:     /**
211:      * Determines whether parameter is defined. Do not call directly.
212:      * @return bool
213:      */
214:     public function __isset($name)
215:     {
216:         return isset($this->params[$name]);
217:     }
218: 
219: 
220:     /**
221:      * Removes a template parameter. Do not call directly.
222:      * @param  string    name
223:      * @return void
224:      */
225:     public function __unset($name)
226:     {
227:         unset($this->params[$name]);
228:     }
229: 
230: }
231: 
Nette 2.4-20161109 API API documentation generated by ApiGen 2.8.0