PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Smarty/sysplugins/smarty_internal_template.php

https://bitbucket.org/hkurosawa/jelly2
PHP | 692 lines | 428 code | 31 blank | 233 comment | 128 complexity | f9b213642b5ddc6bb139e0c690e318cb MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Template
  4. *
  5. * This file contains the Smarty template engine
  6. *
  7. * @package Smarty
  8. * @subpackage Template
  9. * @author Uwe Tews
  10. */
  11. /**
  12. * Main class with template data structures and methods
  13. *
  14. * @package Smarty
  15. * @subpackage Template
  16. *
  17. * @property Smarty_Template_Source $source
  18. * @property Smarty_Template_Compiled $compiled
  19. * @property Smarty_Template_Cached $cached
  20. */
  21. class Smarty_Internal_Template extends Smarty_Internal_TemplateBase {
  22. /**
  23. * cache_id
  24. * @var string
  25. */
  26. public $cache_id = null;
  27. /**
  28. * $compile_id
  29. * @var string
  30. */
  31. public $compile_id = null;
  32. /**
  33. * caching enabled
  34. * @var boolean
  35. */
  36. public $caching = null;
  37. /**
  38. * cache lifetime in seconds
  39. * @var integer
  40. */
  41. public $cache_lifetime = null;
  42. /**
  43. * Template resource
  44. * @var string
  45. */
  46. public $template_resource = null;
  47. /**
  48. * flag if compiled template is invalid and must be (re)compiled
  49. * @var bool
  50. */
  51. public $mustCompile = null;
  52. /**
  53. * flag if template does contain nocache code sections
  54. * @var bool
  55. */
  56. public $has_nocache_code = false;
  57. /**
  58. * special compiled and cached template properties
  59. * @var array
  60. */
  61. public $properties = array('file_dependency' => array(),
  62. 'nocache_hash' => '',
  63. 'function' => array());
  64. /**
  65. * required plugins
  66. * @var array
  67. */
  68. public $required_plugins = array('compiled' => array(), 'nocache' => array());
  69. /**
  70. * Global smarty instance
  71. * @var Smarty
  72. */
  73. public $smarty = null;
  74. /**
  75. * blocks for template inheritance
  76. * @var array
  77. */
  78. public $block_data = array();
  79. /**
  80. * variable filters
  81. * @var array
  82. */
  83. public $variable_filters = array();
  84. /**
  85. * optional log of tag/attributes
  86. * @var array
  87. */
  88. public $used_tags = array();
  89. /**
  90. * internal flag to allow relative path in child template blocks
  91. * @var bool
  92. */
  93. public $allow_relative_path = false;
  94. /**
  95. * internal capture runtime stack
  96. * @var array
  97. */
  98. public $_capture_stack = array(0 => array());
  99. /**
  100. * Create template data object
  101. *
  102. * Some of the global Smarty settings copied to template scope
  103. * It load the required template resources and cacher plugins
  104. *
  105. * @param string $template_resource template resource string
  106. * @param Smarty $smarty Smarty instance
  107. * @param Smarty_Internal_Template $_parent back pointer to parent object with variables or null
  108. * @param mixed $_cache_id cache id or null
  109. * @param mixed $_compile_id compile id or null
  110. * @param bool $_caching use caching?
  111. * @param int $_cache_lifetime cache life-time in seconds
  112. */
  113. public function __construct($template_resource, $smarty, $_parent = null, $_cache_id = null, $_compile_id = null, $_caching = null, $_cache_lifetime = null)
  114. {
  115. $this->smarty = &$smarty;
  116. // Smarty parameter
  117. $this->cache_id = $_cache_id === null ? $this->smarty->cache_id : $_cache_id;
  118. $this->compile_id = $_compile_id === null ? $this->smarty->compile_id : $_compile_id;
  119. $this->caching = $_caching === null ? $this->smarty->caching : $_caching;
  120. if ($this->caching === true)
  121. $this->caching = Smarty::CACHING_LIFETIME_CURRENT;
  122. $this->cache_lifetime = $_cache_lifetime === null ? $this->smarty->cache_lifetime : $_cache_lifetime;
  123. $this->parent = $_parent;
  124. // Template resource
  125. $this->template_resource = $template_resource;
  126. // copy block data of template inheritance
  127. if ($this->parent instanceof Smarty_Internal_Template) {
  128. $this->block_data = $this->parent->block_data;
  129. }
  130. }
  131. /**
  132. * Returns if the current template must be compiled by the Smarty compiler
  133. *
  134. * It does compare the timestamps of template source and the compiled templates and checks the force compile configuration
  135. *
  136. * @return boolean true if the template must be compiled
  137. */
  138. public function mustCompile()
  139. {
  140. if (!$this->source->exists) {
  141. if ($this->parent instanceof Smarty_Internal_Template) {
  142. $parent_resource = " in '$this->parent->template_resource}'";
  143. } else {
  144. $parent_resource = '';
  145. }
  146. throw new SmartyException("Unable to load template {$this->source->type} '{$this->source->name}'{$parent_resource}");
  147. }
  148. if ($this->mustCompile === null) {
  149. $this->mustCompile = (!$this->source->uncompiled && ($this->smarty->force_compile || $this->source->recompiled || $this->compiled->timestamp === false ||
  150. ($this->smarty->compile_check && $this->compiled->timestamp < $this->source->timestamp)));
  151. }
  152. return $this->mustCompile;
  153. }
  154. /**
  155. * Compiles the template
  156. *
  157. * If the template is not evaluated the compiled template is saved on disk
  158. */
  159. public function compileTemplateSource()
  160. {
  161. if (!$this->source->recompiled) {
  162. $this->properties['file_dependency'] = array();
  163. if ($this->source->components) {
  164. // uses real resource for file dependency
  165. $source = end($this->source->components);
  166. $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type);
  167. } else {
  168. $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type);
  169. }
  170. }
  171. if ($this->smarty->debugging) {
  172. Smarty_Internal_Debug::start_compile($this);
  173. }
  174. // compile locking
  175. if ($this->smarty->compile_locking && !$this->source->recompiled) {
  176. if ($saved_timestamp = $this->compiled->timestamp) {
  177. touch($this->compiled->filepath);
  178. }
  179. }
  180. // call compiler
  181. try {
  182. $code = $this->compiler->compileTemplate($this);
  183. } catch (Exception $e) {
  184. // restore old timestamp in case of error
  185. if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) {
  186. touch($this->compiled->filepath, $saved_timestamp);
  187. }
  188. throw $e;
  189. }
  190. // compiling succeded
  191. if (!$this->source->recompiled && $this->compiler->write_compiled_code) {
  192. // write compiled template
  193. $_filepath = $this->compiled->filepath;
  194. if ($_filepath === false)
  195. throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to');
  196. Smarty_Internal_Write_File::writeFile($_filepath, $code, $this->smarty);
  197. $this->compiled->exists = true;
  198. $this->compiled->isCompiled = true;
  199. }
  200. if ($this->smarty->debugging) {
  201. Smarty_Internal_Debug::end_compile($this);
  202. }
  203. // release compiler object to free memory
  204. unset($this->compiler);
  205. }
  206. /**
  207. * Writes the cached template output
  208. *
  209. * @return bool
  210. */
  211. public function writeCachedContent($content)
  212. {
  213. if ($this->source->recompiled || !($this->caching == Smarty::CACHING_LIFETIME_CURRENT || $this->caching == Smarty::CACHING_LIFETIME_SAVED)) {
  214. // don't write cache file
  215. return false;
  216. }
  217. $this->properties['cache_lifetime'] = $this->cache_lifetime;
  218. $this->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
  219. $content = $this->createTemplateCodeFrame($content, true);
  220. $_smarty_tpl = $this;
  221. eval("?>" . $content);
  222. $this->cached->valid = true;
  223. $this->cached->processed = true;
  224. return $this->cached->write($this, $content);
  225. }
  226. /**
  227. * Template code runtime function to get subtemplate content
  228. *
  229. * @param string $template the resource handle of the template file
  230. * @param mixed $cache_id cache id to be used with this template
  231. * @param mixed $compile_id compile id to be used with this template
  232. * @param integer $caching cache mode
  233. * @param integer $cache_lifetime life time of cache data
  234. * @param array $vars optional variables to assign
  235. * @param int $parent_scope scope in which {include} should execute
  236. * @returns string template content
  237. */
  238. public function getSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope)
  239. {
  240. // already in template cache?
  241. if ($this->smarty->allow_ambiguous_resources) {
  242. $_templateId = Smarty_Resource::getUniqueTemplateName($this->smarty, $template) . $cache_id . $compile_id;
  243. } else {
  244. $_templateId = $this->smarty->joined_template_dir . '#' . $template . $cache_id . $compile_id;
  245. }
  246. if (isset($_templateId[150])) {
  247. $_templateId = sha1($_templateId);
  248. }
  249. if (isset($this->smarty->template_objects[$_templateId])) {
  250. // clone cached template object because of possible recursive call
  251. $tpl = clone $this->smarty->template_objects[$_templateId];
  252. $tpl->parent = $this;
  253. $tpl->caching = $caching;
  254. $tpl->cache_lifetime = $cache_lifetime;
  255. } else {
  256. $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
  257. }
  258. // get variables from calling scope
  259. if ($parent_scope == Smarty::SCOPE_LOCAL) {
  260. $tpl->tpl_vars = $this->tpl_vars;
  261. $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
  262. } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
  263. $tpl->tpl_vars = &$this->tpl_vars;
  264. } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
  265. $tpl->tpl_vars = &Smarty::$global_tpl_vars;
  266. } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
  267. $tpl->tpl_vars = &$this->tpl_vars;
  268. } else {
  269. $tpl->tpl_vars = &$scope_ptr->tpl_vars;
  270. }
  271. $tpl->config_vars = $this->config_vars;
  272. if (!empty($data)) {
  273. // set up variable values
  274. foreach ($data as $_key => $_val) {
  275. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  276. }
  277. }
  278. return $tpl->fetch(null, null, null, null, false, false, true);
  279. }
  280. /**
  281. * Template code runtime function to set up an inline subtemplate
  282. *
  283. * @param string $template the resource handle of the template file
  284. * @param mixed $cache_id cache id to be used with this template
  285. * @param mixed $compile_id compile id to be used with this template
  286. * @param integer $caching cache mode
  287. * @param integer $cache_lifetime life time of cache data
  288. * @param array $vars optional variables to assign
  289. * @param int $parent_scope scope in which {include} should execute
  290. * @param string $hash nocache hash code
  291. * @returns string template content
  292. */
  293. public function setupInlineSubTemplate($template, $cache_id, $compile_id, $caching, $cache_lifetime, $data, $parent_scope, $hash)
  294. {
  295. $tpl = new $this->smarty->template_class($template, $this->smarty, $this, $cache_id, $compile_id, $caching, $cache_lifetime);
  296. $tpl->properties['nocache_hash'] = $hash;
  297. // get variables from calling scope
  298. if ($parent_scope == Smarty::SCOPE_LOCAL ) {
  299. $tpl->tpl_vars = $this->tpl_vars;
  300. $tpl->tpl_vars['smarty'] = clone $this->tpl_vars['smarty'];
  301. } elseif ($parent_scope == Smarty::SCOPE_PARENT) {
  302. $tpl->tpl_vars = &$this->tpl_vars;
  303. } elseif ($parent_scope == Smarty::SCOPE_GLOBAL) {
  304. $tpl->tpl_vars = &Smarty::$global_tpl_vars;
  305. } elseif (($scope_ptr = $this->getScopePointer($parent_scope)) == null) {
  306. $tpl->tpl_vars = &$this->tpl_vars;
  307. } else {
  308. $tpl->tpl_vars = &$scope_ptr->tpl_vars;
  309. }
  310. $tpl->config_vars = $this->config_vars;
  311. if (!empty($data)) {
  312. // set up variable values
  313. foreach ($data as $_key => $_val) {
  314. $tpl->tpl_vars[$_key] = new Smarty_variable($_val);
  315. }
  316. }
  317. return $tpl;
  318. }
  319. /**
  320. * Create code frame for compiled and cached templates
  321. *
  322. * @param string $content optional template content
  323. * @param bool $cache flag for cache file
  324. * @return string
  325. */
  326. public function createTemplateCodeFrame($content = '', $cache = false)
  327. {
  328. $plugins_string = '';
  329. // include code for plugins
  330. if (!$cache) {
  331. if (!empty($this->required_plugins['compiled'])) {
  332. $plugins_string = '<?php ';
  333. foreach ($this->required_plugins['compiled'] as $tmp) {
  334. foreach ($tmp as $data) {
  335. $file = addslashes($data['file']);
  336. if (is_Array($data['function'])){
  337. $plugins_string .= "if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n";
  338. } else {
  339. $plugins_string .= "if (!is_callable('{$data['function']}')) include '{$file}';\n";
  340. }
  341. }
  342. }
  343. $plugins_string .= '?>';
  344. }
  345. if (!empty($this->required_plugins['nocache'])) {
  346. $this->has_nocache_code = true;
  347. $plugins_string .= "<?php echo '/*%%SmartyNocache:{$this->properties['nocache_hash']}%%*/<?php \$_smarty = \$_smarty_tpl->smarty; ";
  348. foreach ($this->required_plugins['nocache'] as $tmp) {
  349. foreach ($tmp as $data) {
  350. $file = addslashes($data['file']);
  351. if (is_Array($data['function'])){
  352. $plugins_string .= addslashes("if (!is_callable(array('{$data['function'][0]}','{$data['function'][1]}'))) include '{$file}';\n");
  353. } else {
  354. $plugins_string .= addslashes("if (!is_callable('{$data['function']}')) include '{$file}';\n");
  355. }
  356. }
  357. }
  358. $plugins_string .= "?>/*/%%SmartyNocache:{$this->properties['nocache_hash']}%%*/';?>\n";
  359. }
  360. }
  361. // build property code
  362. $this->properties['has_nocache_code'] = $this->has_nocache_code;
  363. $output = '';
  364. if (!$this->source->recompiled) {
  365. $output = "<?php /*%%SmartyHeaderCode:{$this->properties['nocache_hash']}%%*/";
  366. if ($this->smarty->direct_access_security) {
  367. $output .= "if(!defined('SMARTY_DIR')) exit('no direct access allowed');\n";
  368. }
  369. }
  370. if ($cache) {
  371. // remove compiled code of{function} definition
  372. unset($this->properties['function']);
  373. if (!empty($this->smarty->template_functions)) {
  374. // copy code of {function} tags called in nocache mode
  375. foreach ($this->smarty->template_functions as $name => $function_data) {
  376. if (isset($function_data['called_nocache'])) {
  377. foreach ($function_data['called_functions'] as $func_name) {
  378. $this->smarty->template_functions[$func_name]['called_nocache'] = true;
  379. }
  380. }
  381. }
  382. foreach ($this->smarty->template_functions as $name => $function_data) {
  383. if (isset($function_data['called_nocache'])) {
  384. unset($function_data['called_nocache'], $function_data['called_functions'], $this->smarty->template_functions[$name]['called_nocache']);
  385. $this->properties['function'][$name] = $function_data;
  386. }
  387. }
  388. }
  389. }
  390. $this->properties['version'] = Smarty::SMARTY_VERSION;
  391. if (!isset($this->properties['unifunc'])) {
  392. $this->properties['unifunc'] = 'content_' . str_replace('.', '_', uniqid('', true));
  393. }
  394. if (!$this->source->recompiled) {
  395. $output .= "\$_valid = \$_smarty_tpl->decodeProperties(" . var_export($this->properties, true) . ',' . ($cache ? 'true' : 'false') . "); /*/%%SmartyHeaderCode%%*/?>\n";
  396. $output .= '<?php if ($_valid && !is_callable(\'' . $this->properties['unifunc'] . '\')) {function ' . $this->properties['unifunc'] . '($_smarty_tpl) {?>';
  397. }
  398. $output .= $plugins_string;
  399. $output .= $content;
  400. if (!$this->source->recompiled) {
  401. $output .= '<?php }} ?>';
  402. }
  403. return $output;
  404. }
  405. /**
  406. * This function is executed automatically when a compiled or cached template file is included
  407. *
  408. * - Decode saved properties from compiled template and cache files
  409. * - Check if compiled or cache file is valid
  410. *
  411. * @param array $properties special template properties
  412. * @param bool $cache flag if called from cache file
  413. * @return bool flag if compiled or cache file is valid
  414. */
  415. public function decodeProperties($properties, $cache = false)
  416. {
  417. $this->has_nocache_code = $properties['has_nocache_code'];
  418. $this->properties['nocache_hash'] = $properties['nocache_hash'];
  419. if (isset($properties['cache_lifetime'])) {
  420. $this->properties['cache_lifetime'] = $properties['cache_lifetime'];
  421. }
  422. if (isset($properties['file_dependency'])) {
  423. $this->properties['file_dependency'] = array_merge($this->properties['file_dependency'], $properties['file_dependency']);
  424. }
  425. if (!empty($properties['function'])) {
  426. $this->properties['function'] = array_merge($this->properties['function'], $properties['function']);
  427. $this->smarty->template_functions = array_merge($this->smarty->template_functions, $properties['function']);
  428. }
  429. $this->properties['version'] = (isset($properties['version'])) ? $properties['version'] : '';
  430. $this->properties['unifunc'] = $properties['unifunc'];
  431. // check file dependencies at compiled code
  432. $is_valid = true;
  433. if ($this->properties['version'] != Smarty::SMARTY_VERSION) {
  434. $is_valid = false;
  435. } else if (((!$cache && $this->smarty->compile_check && empty($this->compiled->_properties) && !$this->compiled->isCompiled) || $cache && ($this->smarty->compile_check === true || $this->smarty->compile_check === Smarty::COMPILECHECK_ON)) && !empty($this->properties['file_dependency'])) {
  436. foreach ($this->properties['file_dependency'] as $_file_to_check) {
  437. if ($_file_to_check[2] == 'file' || $_file_to_check[2] == 'php') {
  438. if ($this->source->filepath == $_file_to_check[0] && isset($this->source->timestamp)) {
  439. // do not recheck current template
  440. $mtime = $this->source->timestamp;
  441. } else {
  442. // file and php types can be checked without loading the respective resource handlers
  443. $mtime = @filemtime($_file_to_check[0]);
  444. }
  445. } elseif ($_file_to_check[2] == 'string') {
  446. continue;
  447. } else {
  448. $source = Smarty_Resource::source(null, $this->smarty, $_file_to_check[0]);
  449. $mtime = $source->timestamp;
  450. }
  451. if (!$mtime || $mtime > $_file_to_check[1]) {
  452. $is_valid = false;
  453. break;
  454. }
  455. }
  456. }
  457. if ($cache) {
  458. $this->cached->valid = $is_valid;
  459. } else {
  460. $this->mustCompile = !$is_valid;
  461. }
  462. // store data in reusable Smarty_Template_Compiled
  463. if (!$cache) {
  464. $this->compiled->_properties = $properties;
  465. }
  466. return $is_valid;
  467. }
  468. /**
  469. * Template code runtime function to create a local Smarty variable for array assignments
  470. *
  471. * @param string $tpl_var tempate variable name
  472. * @param bool $nocache cache mode of variable
  473. * @param int $scope scope of variable
  474. */
  475. public function createLocalArrayVariable($tpl_var, $nocache = false, $scope = Smarty::SCOPE_LOCAL)
  476. {
  477. if (!isset($this->tpl_vars[$tpl_var])) {
  478. $this->tpl_vars[$tpl_var] = new Smarty_variable(array(), $nocache, $scope);
  479. } else {
  480. $this->tpl_vars[$tpl_var] = clone $this->tpl_vars[$tpl_var];
  481. if ($scope != Smarty::SCOPE_LOCAL) {
  482. $this->tpl_vars[$tpl_var]->scope = $scope;
  483. }
  484. if (!(is_array($this->tpl_vars[$tpl_var]->value) || $this->tpl_vars[$tpl_var]->value instanceof ArrayAccess)) {
  485. settype($this->tpl_vars[$tpl_var]->value, 'array');
  486. }
  487. }
  488. }
  489. /**
  490. * Template code runtime function to get pointer to template variable array of requested scope
  491. *
  492. * @param int $scope requested variable scope
  493. * @return array array of template variables
  494. */
  495. public function &getScope($scope)
  496. {
  497. if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
  498. return $this->parent->tpl_vars;
  499. } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
  500. $ptr = $this->parent;
  501. while (!empty($ptr->parent)) {
  502. $ptr = $ptr->parent;
  503. }
  504. return $ptr->tpl_vars;
  505. } elseif ($scope == Smarty::SCOPE_GLOBAL) {
  506. return Smarty::$global_tpl_vars;
  507. }
  508. $null = null;
  509. return $null;
  510. }
  511. /**
  512. * Get parent or root of template parent chain
  513. *
  514. * @param int $scope pqrent or root scope
  515. * @return mixed object
  516. */
  517. public function getScopePointer($scope)
  518. {
  519. if ($scope == Smarty::SCOPE_PARENT && !empty($this->parent)) {
  520. return $this->parent;
  521. } elseif ($scope == Smarty::SCOPE_ROOT && !empty($this->parent)) {
  522. $ptr = $this->parent;
  523. while (!empty($ptr->parent)) {
  524. $ptr = $ptr->parent;
  525. }
  526. return $ptr;
  527. }
  528. return null;
  529. }
  530. /**
  531. * [util function] counts an array, arrayaccess/traversable or PDOStatement object
  532. *
  533. * @param mixed $value
  534. * @return int the count for arrays and objects that implement countable, 1 for other objects that don't, and 0 for empty elements
  535. */
  536. public function _count($value)
  537. {
  538. if (is_array($value) === true || $value instanceof Countable) {
  539. return count($value);
  540. } elseif ($value instanceof IteratorAggregate) {
  541. // Note: getIterator() returns a Traversable, not an Iterator
  542. // thus rewind() and valid() methods may not be present
  543. return iterator_count($value->getIterator());
  544. } elseif ($value instanceof Iterator) {
  545. return iterator_count($value);
  546. } elseif ($value instanceof PDOStatement) {
  547. return $value->rowCount();
  548. } elseif ($value instanceof Traversable) {
  549. return iterator_count($value);
  550. } elseif ($value instanceof ArrayAccess) {
  551. if ($value->offsetExists(0)) {
  552. return 1;
  553. }
  554. } elseif (is_object($value)) {
  555. return count($value);
  556. }
  557. return 0;
  558. }
  559. /**
  560. * runtime error not matching capture tags
  561. *
  562. */
  563. public function capture_error()
  564. {
  565. throw new SmartyException("Not matching {capture} open/close in \"{$this->template_resource}\"");
  566. }
  567. /**
  568. * Empty cache for this template
  569. *
  570. * @param integer $exp_time expiration time
  571. * @return integer number of cache files deleted
  572. */
  573. public function clearCache($exp_time=null)
  574. {
  575. Smarty_CacheResource::invalidLoadedCache($this->smarty);
  576. return $this->cached->handler->clear($this->smarty, $this->template_name, $this->cache_id, $this->compile_id, $exp_time);
  577. }
  578. /**
  579. * set Smarty property in template context
  580. *
  581. * @param string $property_name property name
  582. * @param mixed $value value
  583. */
  584. public function __set($property_name, $value)
  585. {
  586. switch ($property_name) {
  587. case 'source':
  588. case 'compiled':
  589. case 'cached':
  590. case 'compiler':
  591. $this->$property_name = $value;
  592. return;
  593. // FIXME: routing of template -> smarty attributes
  594. default:
  595. if (property_exists($this->smarty, $property_name)) {
  596. $this->smarty->$property_name = $value;
  597. return;
  598. }
  599. }
  600. throw new SmartyException("invalid template property '$property_name'.");
  601. }
  602. /**
  603. * get Smarty property in template context
  604. *
  605. * @param string $property_name property name
  606. */
  607. public function __get($property_name)
  608. {
  609. switch ($property_name) {
  610. case 'source':
  611. if (strlen($this->template_resource) == 0) {
  612. throw new SmartyException('Missing template name');
  613. }
  614. $this->source = Smarty_Resource::source($this);
  615. // cache template object under a unique ID
  616. // do not cache eval resources
  617. if ($this->source->type != 'eval') {
  618. if ($this->smarty->allow_ambiguous_resources) {
  619. $_templateId = $this->source->unique_resource . $this->cache_id . $this->compile_id;
  620. } else {
  621. $_templateId = $this->smarty->joined_template_dir . '#' . $this->template_resource . $this->cache_id . $this->compile_id;
  622. }
  623. if (isset($_templateId[150])) {
  624. $_templateId = sha1($_templateId);
  625. }
  626. $this->smarty->template_objects[$_templateId] = $this;
  627. }
  628. return $this->source;
  629. case 'compiled':
  630. $this->compiled = $this->source->getCompiled($this);
  631. return $this->compiled;
  632. case 'cached':
  633. if (!class_exists('Smarty_Template_Cached')) {
  634. include SMARTY_SYSPLUGINS_DIR . 'smarty_cacheresource.php';
  635. }
  636. $this->cached = new Smarty_Template_Cached($this);
  637. return $this->cached;
  638. case 'compiler':
  639. $this->smarty->loadPlugin($this->source->compiler_class);
  640. $this->compiler = new $this->source->compiler_class($this->source->template_lexer_class, $this->source->template_parser_class, $this->smarty);
  641. return $this->compiler;
  642. // FIXME: routing of template -> smarty attributes
  643. default:
  644. if (property_exists($this->smarty, $property_name)) {
  645. return $this->smarty->$property_name;
  646. }
  647. }
  648. throw new SmartyException("template property '$property_name' does not exist.");
  649. }
  650. /**
  651. * Template data object destrutor
  652. *
  653. */
  654. public function __destruct()
  655. {
  656. if ($this->smarty->cache_locking && isset($this->cached) && $this->cached->is_locked) {
  657. $this->cached->handler->releaseLock($this->smarty, $this->cached);
  658. }
  659. }
  660. }
  661. ?>