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