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
      • Reflection
      • 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
  • Other releases
  • Nette homepage
  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 extends Nette\Object implements Nette\Application\UI\ITemplate
 18: {
 19:     /** @var Latte\Engine */
 20:     private $latte;
 21: 
 22:     /** @var string */
 23:     private $file;
 24: 
 25:     /** @var array */
 26:     private $params = array();
 27: 
 28: 
 29:     public function __construct(Latte\Engine $latte)
 30:     {
 31:         $this->latte = $latte;
 32:     }
 33: 
 34: 
 35:     /**
 36:      * @return Latte\Engine
 37:      */
 38:     public function getLatte()
 39:     {
 40:         return $this->latte;
 41:     }
 42: 
 43: 
 44:     /**
 45:      * Renders template to output.
 46:      * @return void
 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:      * Renders template to string.
 56:      * @param  can throw exceptions? (hidden parameter)
 57:      * @return string
 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:     /********************* template filters & helpers ****************d*g**/
 76: 
 77: 
 78:     /**
 79:      * Registers run-time filter.
 80:      * @param  string|NULL
 81:      * @param  callable
 82:      * @return self
 83:      */
 84:     public function addFilter($name, $callback)
 85:     {
 86:         return $this->latte->addFilter($name, $callback);
 87:     }
 88: 
 89: 
 90:     /**
 91:      * Alias for addFilter()
 92:      * @deprecated
 93:      */
 94:     public function registerHelper($name, $callback)
 95:     {
 96:         //trigger_error(__METHOD__ . '() is deprecated, use getLatte()->addFilter().', E_USER_DEPRECATED);
 97:         return $this->latte->addFilter($name, $callback);
 98:     }
 99: 
100: 
101:     /**
102:      * Alias for addFilterLoader()
103:      * @deprecated
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:      * Sets translate adapter.
120:      * @return self
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:      * @deprecated
131:      */
132:     public function registerFilter($callback)
133:     {
134:         throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated.');
135:     }
136: 
137: 
138:     /********************* template parameters ****************d*g**/
139: 
140: 
141:     /**
142:      * Sets the path to the template file.
143:      * @param  string
144:      * @return self
145:      */
146:     public function setFile($file)
147:     {
148:         $this->file = $file;
149:         return $this;
150:     }
151: 
152: 
153:     /**
154:      * @return string
155:      */
156:     public function getFile()
157:     {
158:         return $this->file;
159:     }
160: 
161: 
162:     /**
163:      * Adds new template parameter.
164:      * @return self
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:      * Sets all parameters.
178:      * @param  array
179:      * @return self
180:      */
181:     public function setParameters(array $params)
182:     {
183:         $this->params = $params + $this->params;
184:         return $this;
185:     }
186: 
187: 
188:     /**
189:      * Returns array of all parameters.
190:      * @return array
191:      */
192:     public function getParameters()
193:     {
194:         return $this->params;
195:     }
196: 
197: 
198:     /**
199:      * @deprecated
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:      * Sets a template parameter. Do not call directly.
210:      * @return void
211:      */
212:     public function __set($name, $value)
213:     {
214:         $this->params[$name] = $value;
215:     }
216: 
217: 
218:     /**
219:      * Returns a template parameter. Do not call directly.
220:      * @return mixed  value
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:      * Determines whether parameter is defined. Do not call directly.
234:      * @return bool
235:      */
236:     public function __isset($name)
237:     {
238:         return isset($this->params[$name]);
239:     }
240: 
241: 
242:     /**
243:      * Removes a template parameter. Do not call directly.
244:      * @param  string    name
245:      * @return void
246:      */
247:     public function __unset($name)
248:     {
249:         unset($this->params[$name]);
250:     }
251: 
252: }
253: 
Nette 2.3-20161221 API API documentation generated by ApiGen 2.8.0