You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_template.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Template
  4. * This file contains the Smarty template engine
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Main class with template data structures and methods
  12. *
  13. * @package Smarty
  14. * @subpackage Template
  15. *
  16. * @property Smarty_Template_Source|Smarty_Template_Config $source
  17. * @property Smarty_Template_Compiled $compiled
  18. * @property Smarty_Template_Cached $cached
  19. * @method bool mustCompile()
  20. */
  21. class Smarty_Internal_Template extends Smarty_Internal_TemplateBase
  22. {
  23. /**
  24. * This object type (Smarty = 1, template = 2, data = 4)
  25. *
  26. * @var int
  27. */
  28. public $_objType = 2;
  29. /**
  30. * Global smarty instance
  31. *
  32. * @var Smarty
  33. */
  34. public $smarty = null;
  35. /**
  36. * Source instance
  37. *
  38. * @var Smarty_Template_Source|Smarty_Template_Config
  39. */
  40. public $source = null;
  41. /**
  42. * Template resource
  43. *
  44. * @var string
  45. */
  46. public $template_resource = null;
  47. /**
  48. * flag if compiled template is invalid and must be (re)compiled
  49. *
  50. * @var bool
  51. */
  52. public $mustCompile = null;
  53. /**
  54. * Template Id
  55. *
  56. * @var null|string
  57. */
  58. public $templateId = null;
  59. /**
  60. * Known template functions
  61. *
  62. * @var array
  63. */
  64. public $tpl_function = array();
  65. /**
  66. * Scope in which template is rendered
  67. *
  68. * @var int
  69. */
  70. public $scope = 0;
  71. /**
  72. * Create template data object
  73. * Some of the global Smarty settings copied to template scope
  74. * It load the required template resources and caching plugins
  75. *
  76. * @param string $template_resource template resource string
  77. * @param Smarty $smarty Smarty instance
  78. * @param \Smarty_Internal_Template|\Smarty|\Smarty_Internal_Data $_parent back pointer to parent object
  79. * with variables or null
  80. * @param mixed $_cache_id cache id or null
  81. * @param mixed $_compile_id compile id or null
  82. * @param bool $_caching use caching?
  83. * @param int $_cache_lifetime cache life-time in seconds
  84. *
  85. * @throws \SmartyException
  86. */
  87. public function __construct($template_resource, Smarty $smarty, Smarty_Internal_Data $_parent = null,
  88. $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
  89. {
  90. $this->smarty = &$smarty;
  91. // Smarty parameter
  92. $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
  93. $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
  94. $this->caching = $_caching === null ? $this->smarty->caching : $_caching;
  95. if ($this->caching === true) {
  96. $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
  97. }
  98. $this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
  99. $this->parent = $_parent;
  100. // Template resource
  101. $this->template_resource = $template_resource;
  102. $this->source = Smarty_Template_Source::load($this);
  103. parent::__construct();
  104. }
  105. /**
  106. * render template
  107. *
  108. * @param bool $merge_tpl_vars if true parent template variables merged in to local scope
  109. * @param bool $no_output_filter if true do not run output filter
  110. * @param bool $display true: display, false: fetch null: subtemplate
  111. *
  112. * @throws Exception
  113. * @throws SmartyException
  114. * @return string rendered template output
  115. */
  116. public function render($no_output_filter = true, $display = null)
  117. {
  118. $parentIsTpl = isset($this->parent) && $this->parent->_objType == 2;
  119. if ($this->smarty->debugging) {
  120. $this->smarty->_debug->start_template($this, $display);
  121. }
  122. // checks if template exists
  123. if (!$this->source->exists) {
  124. if ($parentIsTpl) {
  125. $parent_resource = " in '{$this->parent->template_resource}'";
  126. } else {
  127. $parent_resource = '';
  128. }
  129. throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
  130. }
  131. // disable caching for evaluated code
  132. if ($this->source->handler->recompiled) {
  133. $this->caching = false;
  134. }
  135. // read from cache or render
  136. $isCacheTpl =
  137. $this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED;
  138. if ($isCacheTpl) {
  139. if (!isset($this->cached)) {
  140. $this->loadCached();
  141. }
  142. $this->cached->render($this, $no_output_filter);
  143. } elseif ($this->source->handler->uncompiled) {
  144. $this->source->render($this);
  145. } else {
  146. if (!isset($this->compiled)) {
  147. $this->loadCompiled();
  148. }
  149. $this->compiled->render($this);
  150. }
  151. // display or fetch
  152. if ($display) {
  153. if ($this->caching && $this->smarty->cache_modified_check) {
  154. $this->smarty->ext->_cachemodify->cacheModifiedCheck($this->cached, $this,
  155. isset($content) ? $content : ob_get_clean());
  156. } else {
  157. if ((!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) &&
  158. !$no_output_filter && (isset($this->smarty->autoload_filters['output']) ||
  159. isset($this->smarty->registered_filters['output']))
  160. ) {
  161. echo $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this);
  162. } else {
  163. ob_end_flush();
  164. flush();
  165. }
  166. }
  167. if ($this->smarty->debugging) {
  168. $this->smarty->_debug->end_template($this);
  169. // debug output
  170. $this->smarty->_debug->display_debug($this, true);
  171. }
  172. return '';
  173. } else {
  174. if ($this->smarty->debugging) {
  175. $this->smarty->_debug->end_template($this);
  176. if ($this->smarty->debugging === 2 && $display === false) {
  177. $this->smarty->_debug->display_debug($this, true);
  178. }
  179. }
  180. if ($parentIsTpl) {
  181. if (!empty($this->tpl_function)) {
  182. $this->parent->tpl_function = array_merge($this->parent->tpl_function, $this->tpl_function);
  183. }
  184. foreach ($this->compiled->required_plugins as $code => $tmp1) {
  185. foreach ($tmp1 as $name => $tmp) {
  186. foreach ($tmp as $type => $data) {
  187. $this->parent->compiled->required_plugins[$code][$name][$type] = $data;
  188. }
  189. }
  190. }
  191. }
  192. if (!$no_output_filter &&
  193. (!$this->caching || $this->cached->has_nocache_code || $this->source->handler->recompiled) &&
  194. (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))
  195. ) {
  196. return $this->smarty->ext->_filterHandler->runFilter('output', ob_get_clean(), $this);
  197. }
  198. // return cache content
  199. return null;
  200. }
  201. }
  202. /**
  203. * Compiles the template
  204. * If the template is not evaluated the compiled template is saved on disk
  205. */
  206. public function compileTemplateSource()
  207. {
  208. return $this->compiled->compileTemplateSource($this);
  209. }
  210. /**
  211. * Writes the content to cache resource
  212. *
  213. * @param string $content
  214. *
  215. * @return bool
  216. */
  217. public function writeCachedContent($content)
  218. {
  219. return $this->smarty->ext->_updateCache->writeCachedContent($this->cached, $this, $content);
  220. }
  221. /**
  222. * Get unique template id
  223. *
  224. * @return string
  225. */
  226. public function _getTemplateId()
  227. {
  228. return isset($this->templateId) ? $this->templateId : $this->templateId =
  229. $this->smarty->_getTemplateId($this->template_resource, $this->cache_id, $this->compile_id);
  230. }
  231. /**
  232. * runtime error not matching capture tags
  233. */
  234. public function capture_error()
  235. {
  236. throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
  237. }
  238. /**
  239. * Load compiled object
  240. *
  241. */
  242. public function loadCompiled()
  243. {
  244. if (!isset($this->compiled)) {
  245. $this->compiled = Smarty_Template_Compiled::load($this);
  246. }
  247. }
  248. /**
  249. * Load cached object
  250. *
  251. */
  252. public function loadCached()
  253. {
  254. if (!isset($this->cached)) {
  255. $this->cached = Smarty_Template_Cached::load($this);
  256. }
  257. }
  258. /**
  259. * Load compiler object
  260. *
  261. * @throws \SmartyException
  262. */
  263. public function loadCompiler()
  264. {
  265. if (!class_exists($this->source->handler->compiler_class)) {
  266. $this->smarty->loadPlugin($this->source->handler->compiler_class);
  267. }
  268. $this->compiler = new $this->source->handler->compiler_class($this->source->handler->template_lexer_class,
  269. $this->source->handler->template_parser_class,
  270. $this->smarty);
  271. }
  272. /**
  273. * Handle unknown class methods
  274. *
  275. * @param string $name unknown method-name
  276. * @param array $args argument array
  277. *
  278. * @return mixed
  279. * @throws SmartyException
  280. */
  281. public function __call($name, $args)
  282. {
  283. // method of Smarty object?
  284. if (method_exists($this->smarty, $name)) {
  285. return call_user_func_array(array($this->smarty, $name), $args);
  286. }
  287. // parent
  288. return parent::__call($name, $args);
  289. }
  290. /**
  291. * set Smarty property in template context
  292. *
  293. * @param string $property_name property name
  294. * @param mixed $value value
  295. *
  296. * @throws SmartyException
  297. */
  298. public function __set($property_name, $value)
  299. {
  300. switch ($property_name) {
  301. case 'compiled':
  302. case 'cached':
  303. case 'compiler':
  304. $this->$property_name = $value;
  305. return;
  306. default:
  307. // Smarty property ?
  308. if (property_exists($this->smarty, $property_name)) {
  309. $this->smarty->$property_name = $value;
  310. return;
  311. }
  312. }
  313. throw new SmartyException("invalid template property '$property_name'.");
  314. }
  315. /**
  316. * get Smarty property in template context
  317. *
  318. * @param string $property_name property name
  319. *
  320. * @return mixed|Smarty_Template_Cached
  321. * @throws SmartyException
  322. */
  323. public function __get($property_name)
  324. {
  325. switch ($property_name) {
  326. case 'compiled':
  327. $this->loadCompiled();
  328. return $this->compiled;
  329. case 'cached':
  330. $this->loadCached();
  331. return $this->cached;
  332. case 'compiler':
  333. $this->loadCompiler();
  334. return $this->compiler;
  335. default:
  336. // Smarty property ?
  337. if (property_exists($this->smarty, $property_name)) {
  338. return $this->smarty->$property_name;
  339. }
  340. }
  341. throw new SmartyException("template property '$property_name' does not exist.");
  342. }
  343. /**
  344. * Template data object destructor
  345. */
  346. public function __destruct()
  347. {
  348. if ($this->smarty->cache_locking && isset($this->cached) && $this->cached->is_locked) {
  349. $this->cached->handler->releaseLock($this->smarty, $this->cached);
  350. }
  351. }
  352. }