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