Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

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:  * @author     David Grudl
 18:  */
 19: class Template extends Nette\Object implements Nette\Application\UI\ITemplate
 20: {
 21:     /** @var Latte\Engine */
 22:     private $latte;
 23: 
 24:     /** @var string */
 25:     private $file;
 26: 
 27:     /** @var array */
 28:     private $params = array();
 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 = array())
 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:      * Alias for addFilterLoader()
105:      * @deprecated
106:      */
107:     public function registerHelperLoader($loader)
108:     {
109:         //trigger_error(__METHOD__ . '() is deprecated, use dynamic getLatte()->addFilter().', E_USER_DEPRECATED);
110:         $latte = $this->latte;
111:         $this->latte->addFilter(NULL, function ($name) use ($loader, $latte) {
112:             if ($callback = call_user_func($loader, $name)) {
113:                 $latte->addFilter($name, $callback);
114:             }
115:         });
116:         return $this;
117:     }
118: 
119: 
120:     /**
121:      * Sets translate adapter.
122:      * @return self
123:      */
124:     public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
125:     {
126:         $this->latte->addFilter('translate', $translator === NULL ? NULL : array($translator, 'translate'));
127:         return $this;
128:     }
129: 
130: 
131:     /**
132:      * @deprecated
133:      */
134:     public function registerFilter($callback)
135:     {
136:         throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated.');
137:     }
138: 
139: 
140:     /********************* template parameters ****************d*g**/
141: 
142: 
143:     /**
144:      * Sets the path to the template file.
145:      * @param  string
146:      * @return self
147:      */
148:     public function setFile($file)
149:     {
150:         $this->file = $file;
151:         return $this;
152:     }
153: 
154: 
155:     /**
156:      * @return string
157:      */
158:     public function getFile()
159:     {
160:         return $this->file;
161:     }
162: 
163: 
164:     /**
165:      * Adds new template parameter.
166:      * @return self
167:      */
168:     public function add($name, $value)
169:     {
170:         if (array_key_exists($name, $this->params)) {
171:             throw new Nette\InvalidStateException("The variable '$name' already exists.");
172:         }
173:         $this->params[$name] = $value;
174:         return $this;
175:     }
176: 
177: 
178:     /**
179:      * Sets all parameters.
180:      * @param  array
181:      * @return self
182:      */
183:     public function setParameters(array $params)
184:     {
185:         $this->params = $params + $this->params;
186:         return $this;
187:     }
188: 
189: 
190:     /**
191:      * Returns array of all parameters.
192:      * @return array
193:      */
194:     public function getParameters()
195:     {
196:         return $this->params;
197:     }
198: 
199: 
200:     /**
201:      * @deprecated
202:      */
203:     public function __call($name, $args)
204:     {
205:         return $this->latte->invokeFilter($name, $args);
206:     }
207: 
208: 
209:     /**
210:      * Sets a template parameter. Do not call directly.
211:      * @return void
212:      */
213:     public function __set($name, $value)
214:     {
215:         $this->params[$name] = $value;
216:     }
217: 
218: 
219:     /**
220:      * Returns a template parameter. Do not call directly.
221:      * @return mixed  value
222:      */
223:     public function &__get($name)
224:     {
225:         if (!array_key_exists($name, $this->params)) {
226:             trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
227:         }
228: 
229:         return $this->params[$name];
230:     }
231: 
232: 
233:     /**
234:      * Determines whether parameter is defined. Do not call directly.
235:      * @return bool
236:      */
237:     public function __isset($name)
238:     {
239:         return isset($this->params[$name]);
240:     }
241: 
242: 
243:     /**
244:      * Removes a template parameter. Do not call directly.
245:      * @param  string    name
246:      * @return void
247:      */
248:     public function __unset($name)
249:     {
250:         unset($this->params[$name]);
251:     }
252: 
253: }
254: 
Nette 2.2 API documentation generated by ApiGen 2.8.0