Source for file Template.php

Documentation is available at Template.php

  1. 1: <?php
  2. 2:  
  3. 3: /**
  4. 4:  * Nette Framework
  5. 5:  *
  6. 6:  * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  7. 7:  *
  8. 8:  * This source file is subject to the "Nette license" that is bundled
  9. 9:  * with this package in the file license.txt.
  10. 10:  *
  11. 11:  * For more information please see https://nette.org
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    https://nette.org/license  Nette license
  15. 15:  * @link       https://nette.org
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  * @version    $Id$
  19. 19:  */
  20. 20:  
  21. 21:  
  22. 22:  
  23. 23: require_once dirname(__FILE__'/../Object.php';
  24. 24:  
  25. 25: require_once dirname(__FILE__'/../Templates/IFileTemplate.php';
  26. 26:  
  27. 27:  
  28. 28:  
  29. 29: /**
  30. 30:  * Template.
  31. 31:  *
  32. 32:  * @author     David Grudl
  33. 33:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  34. 34:  * @package    Nette\Templates
  35. 35:  */
  36. 36: class Template extends Object implements IFileTemplate
  37. 37: {
  38. 38:     /** @var bool */
  39. 39:     public $warnOnUndefined = TRUE;
  40. 40:  
  41. 41:     /** @var string */
  42. 42:     private $file;
  43. 43:  
  44. 44:     /** @var array */
  45. 45:     private $params array();
  46. 46:  
  47. 47:     /** @var array */
  48. 48:     private $filters array();
  49. 49:  
  50. 50:     /** @var array */
  51. 51:     private $helpers array();
  52. 52:  
  53. 53:     /** @var array */
  54. 54:     private $helperLoaders array();
  55. 55:  
  56. 56:     /** @var int */
  57. 57:     public static $cacheExpire FALSE;
  58. 58:  
  59. 59:     /** @var ICacheStorage */
  60. 60:     private static $cacheStorage;
  61. 61:  
  62. 62:  
  63. 63:  
  64. 64:     /**
  65. 65:      * Sets the path to the template file.
  66. 66:      * @param  string  template file path
  67. 67:      * @return void 
  68. 68:      */
  69. 69:     public function setFile($file)
  70. 70:     {
  71. 71:         $this->file $file;
  72. 72:     }
  73. 73:  
  74. 74:  
  75. 75:  
  76. 76:     /**
  77. 77:      * Returns the path to the template file.
  78. 78:      * @return string  template file path
  79. 79:      */
  80. 80:     public function getFile()
  81. 81:     {
  82. 82:         return $this->file;
  83. 83:     }
  84. 84:  
  85. 85:  
  86. 86:  
  87. 87:     /**
  88. 88:      * Creates subtemplate.
  89. 89:      * @param  string  file name
  90. 90:      * @param  array   parameters (optional)
  91. 91:      * @return Template 
  92. 92:      */
  93. 93:     public function subTemplate($filearray $params NULL)
  94. 94:     {
  95. 95:         if ($file instanceof self{
  96. 96:             $tpl $file;
  97. 97:  
  98. 98:         elseif ($file == NULL// intentionally ==
  99. 99:             throw new InvalidArgumentException("Template file name was not specified.");
  100. 100:  
  101. 101:         else {
  102. 102:             $tpl clone $this;
  103. 103:             if ($file[0!== '/' && $file[1!== ':'{
  104. 104:                 $file dirname($this->file'/' $file;
  105. 105:             }
  106. 106:             $tpl->setFile($file);
  107. 107:         }
  108. 108:  
  109. 109:         if ($params === NULL{
  110. 110:             $tpl->params $this->params;
  111. 111:  
  112. 112:         else {
  113. 113:             $tpl->params $params;
  114. 114:             $tpl->params += $this->params;
  115. 115:         }
  116. 116:  
  117. 117:         return $tpl;
  118. 118:     }
  119. 119:  
  120. 120:  
  121. 121:  
  122. 122:     /**
  123. 123:      * Registers callback as template filter.
  124. 124:      * @param  callback 
  125. 125:      * @return void 
  126. 126:      */
  127. 127:     public function registerFilter($callback)
  128. 128:     {
  129. 129:         fixCallback($callback);
  130. 130:         if (in_array($callback$this->filtersTRUE)) {
  131. 131:             is_callable($callbackTRUE$textual);
  132. 132:             throw new InvalidStateException("Filter '$textual' was registered twice.");
  133. 133:         }
  134. 134:         $this->filters[$callback;
  135. 135:     }
  136. 136:  
  137. 137:  
  138. 138:  
  139. 139:     /********************* rendering ****************d*g**/
  140. 140:  
  141. 141:  
  142. 142:  
  143. 143:     /**
  144. 144:      * Renders template to output.
  145. 145:      * @return void 
  146. 146:      */
  147. 147:     public function render()
  148. 148:     {
  149. 149:         if ($this->file == NULL// intentionally ==
  150. 150:             throw new InvalidStateException("Template file name was not specified.");
  151. 151:  
  152. 152:         elseif (!is_file($this->file|| !is_readable($this->file)) {
  153. 153:             throw new FileNotFoundException("Missing template file '$this->file'.");
  154. 154:         }
  155. 155:  
  156. 156:         $this->params['template'$this;
  157. 157:  
  158. 158:         if (!count($this->filters)) {
  159. 159:             LimitedScope::load($this->file$this->params);
  160. 160:             return;
  161. 161:         }
  162. 162:  
  163. 163:         $cache new Cache($this->getCacheStorage()'Nette.Template');
  164. 164:         $key md5($this->filecount($this->filters'.' basename($this->file);
  165. 165:         $cached $content $cache[$key];
  166. 166:  
  167. 167:         if ($content === NULL{
  168. 168:             $content file_get_contents($this->file);
  169. 169:  
  170. 170:             foreach ($this->filters as $filter{
  171. 171:                 if (!is_callable($filter)) {
  172. 172:                     $able is_callable($filterTRUE$textual);
  173. 173:                     throw new InvalidStateException("Filter '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  174. 174:                 }
  175. 175:  
  176. 176:                 // remove PHP code
  177. 177:                 $res '';
  178. 178:                 $blocks array();
  179. 179:                 unset($php);
  180. 180:                 foreach (token_get_all($contentas $token{
  181. 181:                     if (is_array($token)) {
  182. 182:                         if ($token[0=== T_INLINE_HTML{
  183. 183:                             $res .= $token[1];
  184. 184:                             unset($php);
  185. 185:                         else {
  186. 186:                             if (!isset($php)) {
  187. 187:                                 $res .= $php "\x01@php:p" count($blocks"@\x02";
  188. 188:                                 $php $blocks[$php];
  189. 189:                             }
  190. 190:                             $php .= $token[1];
  191. 191:                         }
  192. 192:                     else {
  193. 193:                         $php .= $token;
  194. 194:                     }
  195. 195:                 }
  196. 196:  
  197. 197:                 try {
  198. 198:                     $content call_user_func($filter$res);
  199. 199:                 catch (Exception $e{
  200. 200:                     is_callable($filterTRUE$textual);
  201. 201:                     throw new InvalidStateException("Filter $textual$e->getMessage(" (in file $this->file)"0$e);
  202. 202:                 }
  203. 203:  
  204. 204:                 $content strtr($content$blocks)// put PHP code back
  205. 205:             }
  206. 206:  
  207. 207:             $content "<?php\n// template $this->file\n?>$content";
  208. 208:             $cache->save(
  209. 209:                 $key,
  210. 210:                 $content,
  211. 211:                 array(
  212. 212:                     Cache::FILES => $this->file,
  213. 213:                     Cache::EXPIRE => self::$cacheExpire,
  214. 214:                 )
  215. 215:             );
  216. 216:             $cached $cache[$key];
  217. 217:         }
  218. 218:  
  219. 219:         if ($cached !== NULL && self::$cacheStorage instanceof TemplateCacheStorage{
  220. 220:             LimitedScope::load($cached['file']$this->params);
  221. 221:             fclose($cached['handle']);
  222. 222:  
  223. 223:         else {
  224. 224:             LimitedScope::evaluate($content$this->params);
  225. 225:         }
  226. 226:     }
  227. 227:  
  228. 228:  
  229. 229:  
  230. 230:     /**
  231. 231:      * Renders template to string.
  232. 232:      * @param bool  can throw exceptions? (hidden parameter)
  233. 233:      * @return string 
  234. 234:      */
  235. 235:     public function __toString()
  236. 236:     {
  237. 237:         ob_start();
  238. 238:         try {
  239. 239:             $this->render();
  240. 240:             return ob_get_clean();
  241. 241:  
  242. 242:         catch (Exception $e{
  243. 243:             ob_end_clean();
  244. 244:             if (func_num_args(&& func_get_arg(0)) {
  245. 245:                 throw $e;
  246. 246:             else {
  247. 247:                 trigger_error($e->getMessage()E_USER_WARNING);
  248. 248:                 return '';
  249. 249:             }
  250. 250:         }
  251. 251:     }
  252. 252:  
  253. 253:  
  254. 254:  
  255. 255:     /**
  256. 256:      * Converts to SimpleXML. (experimental)
  257. 257:      * @return SimpleXMLElement 
  258. 258:      */
  259. 259:     public function toXml()
  260. 260:     {
  261. 261:         $dom new DOMDocument;
  262. 262:         $dom->loadHTML('<html><meta http-equiv="Content-Type" content="text/html;charset=utf-8">' str_replace("\r"''$this->__toString()) '</html>');
  263. 263:         return simplexml_import_dom($dom)->body;
  264. 264:         //return simplexml_load_string('<xml>' . $this->__toString() . '</xml>');
  265. 265:     }
  266. 266:  
  267. 267:  
  268. 268:  
  269. 269:     /********************* template helpers ****************d*g**/
  270. 270:  
  271. 271:  
  272. 272:  
  273. 273:     /**
  274. 274:      * Registers callback as template helper.
  275. 275:      * @param  string 
  276. 276:      * @param  callback 
  277. 277:      * @return void 
  278. 278:      */
  279. 279:     public function registerHelper($name$callback)
  280. 280:     {
  281. 281:         fixCallback($callback);
  282. 282:         if (!is_callable($callback)) {
  283. 283:             $able is_callable($callbackTRUE$textual);
  284. 284:             throw new InvalidArgumentException("Helper handler '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  285. 285:         }
  286. 286:         $this->helpers[strtolower($name)$callback;
  287. 287:     }
  288. 288:  
  289. 289:  
  290. 290:  
  291. 291:     /**
  292. 292:      * Registers callback as template helpers loader.
  293. 293:      * @param  callback 
  294. 294:      * @return void 
  295. 295:      */
  296. 296:     public function registerHelperLoader($callback)
  297. 297:     {
  298. 298:         fixCallback($callback);
  299. 299:         if (!is_callable($callback)) {
  300. 300:             $able is_callable($callbackTRUE$textual);
  301. 301:             throw new InvalidArgumentException("Helper loader '$textual' is not ($able 'callable.' 'valid PHP callback.'));
  302. 302:         }
  303. 303:         $this->helperLoaders[$callback;
  304. 304:     }
  305. 305:  
  306. 306:  
  307. 307:  
  308. 308:     /**
  309. 309:      * Call a template helper. Do not call directly.
  310. 310:      * @param  string  helper name
  311. 311:      * @param  array   arguments
  312. 312:      * @return mixed 
  313. 313:      */
  314. 314:     public function __call($name$args)
  315. 315:     {
  316. 316:         $name strtolower($name);
  317. 317:         if (!isset($this->helpers[$name])) {
  318. 318:             foreach ($this->helperLoaders as $loader{
  319. 319:                 $helper call_user_func($loader$name);
  320. 320:                 if ($helper{
  321. 321:                     $this->registerHelper($name$helper);
  322. 322:                     return call_user_func_array($helper$args);
  323. 323:                 }
  324. 324:             }
  325. 325:             throw new InvalidStateException("The helper '$name' was not registered.");
  326. 326:         }
  327. 327:  
  328. 328:         return call_user_func_array($this->helpers[$name]$args);
  329. 329:     }
  330. 330:  
  331. 331:  
  332. 332:  
  333. 333:     /**
  334. 334:      * Sets translate adapter.
  335. 335:      * @param  ITranslator 
  336. 336:      * @return void 
  337. 337:      */
  338. 338:     public function setTranslator(ITranslator $translator NULL)
  339. 339:     {
  340. 340:         $this->registerHelper('translate'$translator === NULL NULL array($translator'translate'));
  341. 341:     }
  342. 342:  
  343. 343:  
  344. 344:  
  345. 345:     /********************* template parameters ****************d*g**/
  346. 346:  
  347. 347:  
  348. 348:  
  349. 349:     /**
  350. 350:      * Adds new template parameter.
  351. 351:      * @param  string  name
  352. 352:      * @param  mixed   value
  353. 353:      * @return void 
  354. 354:      */
  355. 355:     public function add($name$value)
  356. 356:     {
  357. 357:         if (array_key_exists($name$this->params)) {
  358. 358:             throw new InvalidStateException("The variable '$name' exists yet.");
  359. 359:         }
  360. 360:  
  361. 361:         $this->params[$name$value;
  362. 362:     }
  363. 363:  
  364. 364:  
  365. 365:  
  366. 366:     /**
  367. 367:      * Adds new template as parameter.
  368. 368:      * @param  string  name
  369. 369:      * @param  string|Template file name or Template object
  370. 370:      * @return Template 
  371. 371:      */
  372. 372:     public function addTemplate($name$file)
  373. 373:     {
  374. 374:         $tpl $this->subTemplate($file);
  375. 375:         $this->add($name$tpl);
  376. 376:         return $tpl;
  377. 377:     }
  378. 378:  
  379. 379:  
  380. 380:  
  381. 381:     /**
  382. 382:      * Returns array of all parameters.
  383. 383:      * @return array 
  384. 384:      */
  385. 385:     public function getParams()
  386. 386:     {
  387. 387:         return $this->params;
  388. 388:     }
  389. 389:  
  390. 390:  
  391. 391:  
  392. 392:     /**
  393. 393:      * Sets a template parameter. Do not call directly.
  394. 394:      * @param  string  name
  395. 395:      * @param  mixed   value
  396. 396:      * @return void 
  397. 397:      */
  398. 398:     public function __set($name$value)
  399. 399:     {
  400. 400:         $this->params[$name$value;
  401. 401:     }
  402. 402:  
  403. 403:  
  404. 404:  
  405. 405:     /**
  406. 406:      * Returns a template parameter. Do not call directly.
  407. 407:      * @param  string  name
  408. 408:      * @return mixed  value
  409. 409:      */
  410. 410:     public function &__get($name)
  411. 411:     {
  412. 412:         if ($this->warnOnUndefined && !array_key_exists($name$this->params)) {
  413. 413:             trigger_error("The variable '$name' does not exist in template '$this->file'"E_USER_WARNING);
  414. 414:         }
  415. 415:  
  416. 416:         return $this->params[$name];
  417. 417:     }
  418. 418:  
  419. 419:  
  420. 420:  
  421. 421:     /**
  422. 422:      * Determines whether parameter is defined. Do not call directly.
  423. 423:      * @param  string    name
  424. 424:      * @return bool 
  425. 425:      */
  426. 426:     public function __isset($name)
  427. 427:     {
  428. 428:         return isset($this->params[$name]);
  429. 429:     }
  430. 430:  
  431. 431:  
  432. 432:  
  433. 433:     /**
  434. 434:      * Removes a template parameter. Do not call directly.
  435. 435:      * @param  string    name
  436. 436:      * @return void 
  437. 437:      */
  438. 438:     public function __unset($name)
  439. 439:     {
  440. 440:         unset($this->params[$name]);
  441. 441:     }
  442. 442:  
  443. 443:  
  444. 444:  
  445. 445:     /********************* caching ****************d*g**/
  446. 446:  
  447. 447:  
  448. 448:  
  449. 449:     /**
  450. 450:      * Set cache storage.
  451. 451:      * @param  Cache 
  452. 452:      * @return void 
  453. 453:      */
  454. 454:     public static function setCacheStorage(ICacheStorage $storage)
  455. 455:     {
  456. 456:         self::$cacheStorage $storage;
  457. 457:     }
  458. 458:  
  459. 459:  
  460. 460:  
  461. 461:     /**
  462. 462:      * @return ICacheStorage 
  463. 463:      */
  464. 464:     public static function getCacheStorage()
  465. 465:     {
  466. 466:         if (self::$cacheStorage === NULL{
  467. 467:             self::$cacheStorage new TemplateCacheStorage(Environment::getVariable('cacheBase'));
  468. 468:         }
  469. 469:         return self::$cacheStorage;
  470. 470:     }
  471. 471: