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
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • 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 (http://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://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 (\Exception $e) {
 64:             if (func_num_args()) {
 65:                 throw $e;
 66:             }
 67:             trigger_error("Exception in " . __METHOD__ . "(): {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}", E_USER_ERROR);
 68:         }
 69:     }
 70: 
 71: 
 72:     /********************* template filters & helpers ****************d*g**/
 73: 
 74: 
 75:     /**
 76:      * Registers run-time filter.
 77:      * @param  string|NULL
 78:      * @param  callable
 79:      * @return self
 80:      */
 81:     public function addFilter($name, $callback)
 82:     {
 83:         return $this->latte->addFilter($name, $callback);
 84:     }
 85: 
 86: 
 87:     /**
 88:      * Alias for addFilter()
 89:      * @deprecated
 90:      */
 91:     public function registerHelper($name, $callback)
 92:     {
 93:         //trigger_error(__METHOD__ . '() is deprecated, use getLatte()->addFilter().', E_USER_DEPRECATED);
 94:         return $this->latte->addFilter($name, $callback);
 95:     }
 96: 
 97: 
 98:     /**
 99:      * Alias for addFilterLoader()
100:      * @deprecated
101:      */
102:     public function registerHelperLoader($loader)
103:     {
104:         trigger_error(__METHOD__ . '() is deprecated, use dynamic getLatte()->addFilter().', E_USER_DEPRECATED);
105:         $latte = $this->latte;
106:         $this->latte->addFilter(NULL, function ($name) use ($loader, $latte) {
107:             if ($callback = call_user_func($loader, $name)) {
108:                 $latte->addFilter($name, $callback);
109:             }
110:         });
111:         return $this;
112:     }
113: 
114: 
115:     /**
116:      * Sets translate adapter.
117:      * @return self
118:      */
119:     public function setTranslator(Nette\Localization\ITranslator $translator = NULL)
120:     {
121:         $this->latte->addFilter('translate', $translator === NULL ? NULL : array($translator, 'translate'));
122:         return $this;
123:     }
124: 
125: 
126:     /**
127:      * @deprecated
128:      */
129:     public function registerFilter($callback)
130:     {
131:         throw new Nette\DeprecatedException(__METHOD__ . '() is deprecated.');
132:     }
133: 
134: 
135:     /********************* template parameters ****************d*g**/
136: 
137: 
138:     /**
139:      * Sets the path to the template file.
140:      * @param  string
141:      * @return self
142:      */
143:     public function setFile($file)
144:     {
145:         $this->file = $file;
146:         return $this;
147:     }
148: 
149: 
150:     /**
151:      * @return string
152:      */
153:     public function getFile()
154:     {
155:         return $this->file;
156:     }
157: 
158: 
159:     /**
160:      * Adds new template parameter.
161:      * @return self
162:      */
163:     public function add($name, $value)
164:     {
165:         if (array_key_exists($name, $this->params)) {
166:             throw new Nette\InvalidStateException("The variable '$name' already exists.");
167:         }
168:         $this->params[$name] = $value;
169:         return $this;
170:     }
171: 
172: 
173:     /**
174:      * Sets all parameters.
175:      * @param  array
176:      * @return self
177:      */
178:     public function setParameters(array $params)
179:     {
180:         $this->params = $params + $this->params;
181:         return $this;
182:     }
183: 
184: 
185:     /**
186:      * Returns array of all parameters.
187:      * @return array
188:      */
189:     public function getParameters()
190:     {
191:         return $this->params;
192:     }
193: 
194: 
195:     /**
196:      * @deprecated
197:      */
198:     public function __call($name, $args)
199:     {
200:         trigger_error('Invoking filters on Template object is deprecated, use getLatte()->invokeFilter().', E_USER_DEPRECATED);
201:         return $this->latte->invokeFilter($name, $args);
202:     }
203: 
204: 
205:     /**
206:      * Sets a template parameter. Do not call directly.
207:      * @return void
208:      */
209:     public function __set($name, $value)
210:     {
211:         $this->params[$name] = $value;
212:     }
213: 
214: 
215:     /**
216:      * Returns a template parameter. Do not call directly.
217:      * @return mixed  value
218:      */
219:     public function &__get($name)
220:     {
221:         if (!array_key_exists($name, $this->params)) {
222:             trigger_error("The variable '$name' does not exist in template.", E_USER_NOTICE);
223:         }
224: 
225:         return $this->params[$name];
226:     }
227: 
228: 
229:     /**
230:      * Determines whether parameter is defined. Do not call directly.
231:      * @return bool
232:      */
233:     public function __isset($name)
234:     {
235:         return isset($this->params[$name]);
236:     }
237: 
238: 
239:     /**
240:      * Removes a template parameter. Do not call directly.
241:      * @param  string    name
242:      * @return void
243:      */
244:     public function __unset($name)
245:     {
246:         unset($this->params[$name]);
247:     }
248: 
249: }
250: 
Nette 2.3.4 API API documentation generated by ApiGen 2.8.0