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 http://nettephp.com
  12. 12:  *
  13. 13:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  14. 14:  * @license    http://nettephp.com/license  Nette license
  15. 15:  * @link       http://nettephp.com
  16. 16:  * @category   Nette
  17. 17:  * @package    Nette\Templates
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../Templates/BaseTemplate.php';
  23. 23:  
  24. 24: require_once dirname(__FILE__'/../Templates/IFileTemplate.php';
  25. 25:  
  26. 26:  
  27. 27:  
  28. 28: /**
  29. 29:  * Template stored in file.
  30. 30:  *
  31. 31:  * @author     David Grudl
  32. 32:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  33. 33:  * @package    Nette\Templates
  34. 34:  */
  35. 35: class Template extends BaseTemplate implements IFileTemplate
  36. 36: {
  37. 37:     /** @var int */
  38. 38:     public static $cacheExpire FALSE;
  39. 39:  
  40. 40:     /** @var ICacheStorage */
  41. 41:     private static $cacheStorage;
  42. 42:  
  43. 43:     /** @var string */
  44. 44:     private $file;
  45. 45:  
  46. 46:  
  47. 47:  
  48. 48:     /**
  49. 49:      * Constructor.
  50. 50:      * @param  string  template file path
  51. 51:      */
  52. 52:     public function __construct($file NULL)
  53. 53:     {
  54. 54:         if ($file !== NULL{
  55. 55:             $this->setFile($file);
  56. 56:         }
  57. 57:     }
  58. 58:  
  59. 59:  
  60. 60:  
  61. 61:     /**
  62. 62:      * Sets the path to the template file.
  63. 63:      * @param  string  template file path
  64. 64:      * @return Template  provides a fluent interface
  65. 65:      */
  66. 66:     public function setFile($file)
  67. 67:     {
  68. 68:         if (!is_file($file)) {
  69. 69:             throw new FileNotFoundException("Missing template file '$file'.");
  70. 70:         }
  71. 71:         $this->file $file;
  72. 72:         return $this;
  73. 73:     }
  74. 74:  
  75. 75:  
  76. 76:  
  77. 77:     /**
  78. 78:      * Returns the path to the template file.
  79. 79:      * @return string  template file path
  80. 80:      */
  81. 81:     public function getFile()
  82. 82:     {
  83. 83:         return $this->file;
  84. 84:     }
  85. 85:  
  86. 86:  
  87. 87:  
  88. 88:     /********************* rendering ****************d*g**/
  89. 89:  
  90. 90:  
  91. 91:  
  92. 92:     /**
  93. 93:      * Renders template to output.
  94. 94:      * @return void 
  95. 95:      */
  96. 96:     public function render()
  97. 97:     {
  98. 98:         if ($this->file == NULL// intentionally ==
  99. 99:             throw new InvalidStateException("Template file name was not specified.");
  100. 100:         }
  101. 101:  
  102. 102:         $this->__set('template'$this);
  103. 103:  
  104. 104:         $cache new Cache($this->getCacheStorage()'Nette.Template');
  105. 105:         $key md5($this->file'.' basename($this->file);
  106. 106:         $cached $content $cache[$key];
  107. 107:  
  108. 108:         if ($content === NULL{
  109. 109:             if (!$this->getFilters()) {
  110. 110:                 $this->onPrepareFilters($this);
  111. 111:             }
  112. 112:  
  113. 113:             if (!$this->getFilters()) {
  114. 114:                 LimitedScope::load($this->file$this->getParams());
  115. 115:                 return;
  116. 116:             }
  117. 117:  
  118. 118:             try {
  119. 119:                 $shortName $this->file;
  120. 120:                 $shortName str_replace(Environment::getVariable('appDir')"\xE2\x80\xA6"$shortName);
  121. 121:             catch (Exception $foo{
  122. 122:             }
  123. 123:  
  124. 124:             $content $this->compile(file_get_contents($this->file)"file $shortName");
  125. 125:             $cache->save(
  126. 126:                 $key,
  127. 127:                 $content,
  128. 128:                 array(
  129. 129:                     Cache::FILES => $this->file,
  130. 130:                     Cache::EXPIRE => self::$cacheExpire,
  131. 131:                 )
  132. 132:             );
  133. 133:             $cached $cache[$key];
  134. 134:         }
  135. 135:  
  136. 136:         if ($cached !== NULL && self::$cacheStorage instanceof TemplateCacheStorage{
  137. 137:             LimitedScope::load($cached['file']$this->getParams());
  138. 138:             fclose($cached['handle']);
  139. 139:  
  140. 140:         else {
  141. 141:             LimitedScope::evaluate($content$this->getParams());
  142. 142:         }
  143. 143:     }
  144. 144:  
  145. 145:  
  146. 146:  
  147. 147:     /********************* caching ****************d*g**/
  148. 148:  
  149. 149:  
  150. 150:  
  151. 151:     /**
  152. 152:      * Set cache storage.
  153. 153:      * @param  Cache 
  154. 154:      * @return void 
  155. 155:      */
  156. 156:     public static function setCacheStorage(ICacheStorage $storage)
  157. 157:     {
  158. 158:         self::$cacheStorage $storage;
  159. 159:     }
  160. 160:  
  161. 161:  
  162. 162:  
  163. 163:     /**
  164. 164:      * @return ICacheStorage 
  165. 165:      */
  166. 166:     public static function getCacheStorage()
  167. 167:     {
  168. 168:         if (self::$cacheStorage === NULL{
  169. 169:             self::$cacheStorage new TemplateCacheStorage(Environment::getVariable('tempDir'));
  170. 170:         }
  171. 171:         return self::$cacheStorage;
  172. 172:     }
  173. 173: