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