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  Copyright (c) 2004, 2010 David Grudl
  7. 7:  * @license    http://nettephp.com/license  Nette license
  8. 8:  * @link       http://nettephp.com
  9. 9:  * @category   Nette
  10. 10:  * @package    Nette\Templates
  11. 11:  */
  12. 12:  
  13. 13:  
  14. 14:  
  15. 15: /**
  16. 16:  * Template stored in file.
  17. 17:  *
  18. 18:  * @copyright  Copyright (c) 2004, 2010 David Grudl
  19. 19:  * @package    Nette\Templates
  20. 20:  */
  21. 21: class Template extends BaseTemplate implements IFileTemplate
  22. 22: {
  23. 23:     /** @var int */
  24. 24:     public static $cacheExpire FALSE;
  25. 25:  
  26. 26:     /** @var ICacheStorage */
  27. 27:     private static $cacheStorage;
  28. 28:  
  29. 29:     /** @var string */
  30. 30:     private $file;
  31. 31:  
  32. 32:  
  33. 33:  
  34. 34:     /**
  35. 35:      * Constructor.
  36. 36:      * @param  string  template file path
  37. 37:      */
  38. 38:     public function __construct($file NULL)
  39. 39:     {
  40. 40:         if ($file !== NULL{
  41. 41:             $this->setFile($file);
  42. 42:         }
  43. 43:     }
  44. 44:  
  45. 45:  
  46. 46:  
  47. 47:     /**
  48. 48:      * Sets the path to the template file.
  49. 49:      * @param  string  template file path
  50. 50:      * @return Template  provides a fluent interface
  51. 51:      */
  52. 52:     public function setFile($file)
  53. 53:     {
  54. 54:         if (!is_file($file)) {
  55. 55:             throw new FileNotFoundException("Missing template file '$file'.");
  56. 56:         }
  57. 57:         $this->file $file;
  58. 58:         return $this;
  59. 59:     }
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Returns the path to the template file.
  65. 65:      * @return string  template file path
  66. 66:      */
  67. 67:     public function getFile()
  68. 68:     {
  69. 69:         return $this->file;
  70. 70:     }
  71. 71:  
  72. 72:  
  73. 73:  
  74. 74:     /********************* rendering ****************d*g**/
  75. 75:  
  76. 76:  
  77. 77:  
  78. 78:     /**
  79. 79:      * Renders template to output.
  80. 80:      * @return void 
  81. 81:      */
  82. 82:     public function render()
  83. 83:     {
  84. 84:         if ($this->file == NULL// intentionally ==
  85. 85:             throw new InvalidStateException("Template file name was not specified.");
  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('appDir')"\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: