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:      * Sets the path to the template file.
  50. 50:      * @param  string  template file path
  51. 51:      * @return void 
  52. 52:      */
  53. 53:     public function setFile($file)
  54. 54:     {
  55. 55:         $this->file $file;
  56. 56:     }
  57. 57:  
  58. 58:  
  59. 59:  
  60. 60:     /**
  61. 61:      * Returns the path to the template file.
  62. 62:      * @return string  template file path
  63. 63:      */
  64. 64:     public function getFile()
  65. 65:     {
  66. 66:         return $this->file;
  67. 67:     }
  68. 68:  
  69. 69:  
  70. 70:  
  71. 71:     /********************* rendering ****************d*g**/
  72. 72:  
  73. 73:  
  74. 74:  
  75. 75:     /**
  76. 76:      * Renders template to output.
  77. 77:      * @return void 
  78. 78:      */
  79. 79:     public function render()
  80. 80:     {
  81. 81:         if ($this->file == NULL// intentionally ==
  82. 82:             throw new InvalidStateException("Template file name was not specified.");
  83. 83:  
  84. 84:         elseif (!is_file($this->file|| !is_readable($this->file)) {
  85. 85:             throw new FileNotFoundException("Missing template file '$this->file'.");
  86. 86:         }
  87. 87:  
  88. 88:         $this->__set('template'$this);
  89. 89:  
  90. 90:         $cache new Cache($this->getCacheStorage()'Nette.Template');
  91. 91:         $key md5($this->file'.' basename($this->file);
  92. 92:         $cached $content $cache[$key];
  93. 93:  
  94. 94:         if ($content === NULL{
  95. 95:             if (!$this->getFilters()) {
  96. 96:                 $this->onPrepareFilters($this);
  97. 97:             }
  98. 98:  
  99. 99:             if (!$this->getFilters()) {
  100. 100:                 LimitedScope::load($this->file$this->getParams());
  101. 101:                 return;
  102. 102:             }
  103. 103:  
  104. 104:             try {
  105. 105:                 $shortName $this->file;
  106. 106:                 $shortName str_replace(Environment::getVariable('templatesDir')"\xE2\x80\xA6"$shortName);
  107. 107:             catch (Exception $foo{
  108. 108:             }
  109. 109:  
  110. 110:             $content $this->compile(file_get_contents($this->file)"file $shortName");
  111. 111:             $cache->save(
  112. 112:                 $key,
  113. 113:                 $content,
  114. 114:                 array(
  115. 115:                     Cache::FILES => $this->file,
  116. 116:                     Cache::EXPIRE => self::$cacheExpire,
  117. 117:                 )
  118. 118:             );
  119. 119:             $cached $cache[$key];
  120. 120:         }
  121. 121:  
  122. 122:         if ($cached !== NULL && self::$cacheStorage instanceof TemplateCacheStorage{
  123. 123:             LimitedScope::load($cached['file']$this->getParams());
  124. 124:             fclose($cached['handle']);
  125. 125:  
  126. 126:         else {
  127. 127:             LimitedScope::evaluate($content$this->getParams());
  128. 128:         }
  129. 129:     }
  130. 130:  
  131. 131:  
  132. 132:  
  133. 133:     /********************* caching ****************d*g**/
  134. 134:  
  135. 135:  
  136. 136:  
  137. 137:     /**
  138. 138:      * Set cache storage.
  139. 139:      * @param  Cache 
  140. 140:      * @return void 
  141. 141:      */
  142. 142:     public static function setCacheStorage(ICacheStorage $storage)
  143. 143:     {
  144. 144:         self::$cacheStorage $storage;
  145. 145:     }
  146. 146:  
  147. 147:  
  148. 148:  
  149. 149:     /**
  150. 150:      * @return ICacheStorage 
  151. 151:      */
  152. 152:     public static function getCacheStorage()
  153. 153:     {
  154. 154:         if (self::$cacheStorage === NULL{
  155. 155:             self::$cacheStorage new TemplateCacheStorage(Environment::getVariable('tempDir'));
  156. 156:         }
  157. 157:         return self::$cacheStorage;
  158. 158:     }
  159. 159: