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_templatebase.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Smarty Template Base
  4. * This file contains the basic shared methods for template handling
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Class with shared smarty/template methods
  12. *
  13. * @package Smarty
  14. * @subpackage Template
  15. *
  16. * @property int $_objType
  17. *
  18. * The following methods will be dynamically loaded by the extension handler when they are called.
  19. * They are located in a corresponding Smarty_Internal_Method_xxxx class
  20. *
  21. * @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
  22. * @method Smarty_Internal_TemplateBase addDefaultModifier(mixed $modifiers)
  23. * @method Smarty_Internal_TemplateBase addLiterals(mixed $literals)
  24. * @method Smarty_Internal_TemplateBase createData(Smarty_Internal_Data $parent = null, string $name = null)
  25. * @method array getAutoloadFilters(string $type = null)
  26. * @method string getDebugTemplate()
  27. * @method array getDefaultModifier()
  28. * @method array getLiterals()
  29. * @method array getTags(mixed $template = null)
  30. * @method object getRegisteredObject(string $object_name)
  31. * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
  32. * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
  33. * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
  34. * @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
  35. * @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
  36. * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
  37. * @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
  38. * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
  39. * @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers)
  40. * @method Smarty_Internal_TemplateBase setLiterals(mixed $literals)
  41. * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
  42. * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
  43. * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
  44. * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
  45. * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
  46. * @method Smarty_Internal_TemplateBase unregisterResource(string $name)
  47. */
  48. abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
  49. {
  50. /**
  51. * Set this if you want different sets of cache files for the same
  52. * templates.
  53. *
  54. * @var string
  55. */
  56. public $cache_id = null;
  57. /**
  58. * Set this if you want different sets of compiled files for the same
  59. * templates.
  60. *
  61. * @var string
  62. */
  63. public $compile_id = null;
  64. /**
  65. * caching enabled
  66. *
  67. * @var int
  68. */
  69. public $caching = Smarty::CACHING_OFF;
  70. /**
  71. * check template for modifications?
  72. *
  73. * @var int
  74. */
  75. public $compile_check = Smarty::COMPILECHECK_ON;
  76. /**
  77. * cache lifetime in seconds
  78. *
  79. * @var integer
  80. */
  81. public $cache_lifetime = 3600;
  82. /**
  83. * Array of source information for known template functions
  84. *
  85. * @var array
  86. */
  87. public $tplFunctions = array();
  88. /**
  89. * universal cache
  90. *
  91. * @var array()
  92. */
  93. public $_cache = array();
  94. /**
  95. * fetches a rendered Smarty template
  96. *
  97. * @param string $template the resource handle of the template file or template object
  98. * @param mixed $cache_id cache id to be used with this template
  99. * @param mixed $compile_id compile id to be used with this template
  100. * @param object $parent next higher level of Smarty variables
  101. *
  102. * @throws Exception
  103. * @throws SmartyException
  104. * @return string rendered template output
  105. */
  106. public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
  107. {
  108. $result = $this->_execute($template, $cache_id, $compile_id, $parent, 0);
  109. return $result === null ? ob_get_clean() : $result;
  110. }
  111. /**
  112. * displays a Smarty template
  113. *
  114. * @param string $template the resource handle of the template file or template object
  115. * @param mixed $cache_id cache id to be used with this template
  116. * @param mixed $compile_id compile id to be used with this template
  117. * @param object $parent next higher level of Smarty variables
  118. *
  119. * @throws \Exception
  120. * @throws \SmartyException
  121. */
  122. public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
  123. {
  124. // display template
  125. $this->_execute($template, $cache_id, $compile_id, $parent, 1);
  126. }
  127. /**
  128. * test if cache is valid
  129. *
  130. * @api Smarty::isCached()
  131. * @link http://www.smarty.net/docs/en/api.is.cached.tpl
  132. *
  133. * @param null|string|\Smarty_Internal_Template $template the resource handle of the template file or template object
  134. * @param mixed $cache_id cache id to be used with this template
  135. * @param mixed $compile_id compile id to be used with this template
  136. * @param object $parent next higher level of Smarty variables
  137. *
  138. * @return bool cache status
  139. * @throws \Exception
  140. * @throws \SmartyException
  141. */
  142. public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
  143. {
  144. return $this->_execute($template, $cache_id, $compile_id, $parent, 2);
  145. }
  146. /**
  147. * fetches a rendered Smarty template
  148. *
  149. * @param string $template the resource handle of the template file or template object
  150. * @param mixed $cache_id cache id to be used with this template
  151. * @param mixed $compile_id compile id to be used with this template
  152. * @param object $parent next higher level of Smarty variables
  153. * @param string $function function type 0 = fetch, 1 = display, 2 = isCache
  154. *
  155. * @return mixed
  156. * @throws \Exception
  157. * @throws \SmartyException
  158. */
  159. private function _execute($template, $cache_id, $compile_id, $parent, $function)
  160. {
  161. $smarty = $this->_getSmartyObj();
  162. $saveVars = true;
  163. if ($template === null) {
  164. if (!$this->_isTplObj()) {
  165. throw new SmartyException($function . '():Missing \'$template\' parameter');
  166. } else {
  167. $template = $this;
  168. }
  169. } elseif (is_object($template)) {
  170. /* @var Smarty_Internal_Template $template */
  171. if (!isset($template->_objType) || !$template->_isTplObj()) {
  172. throw new SmartyException($function . '():Template object expected');
  173. }
  174. } else {
  175. // get template object
  176. $saveVars = false;
  177. $template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent ? $parent : $this, false);
  178. if ($this->_objType === 1) {
  179. // set caching in template object
  180. $template->caching = $this->caching;
  181. }
  182. }
  183. // make sure we have integer values
  184. $template->caching = (int)$template->caching;
  185. // fetch template content
  186. $level = ob_get_level();
  187. try {
  188. $_smarty_old_error_level =
  189. isset($smarty->error_reporting) ? error_reporting($smarty->error_reporting) : null;
  190. if ($this->_objType === 2) {
  191. /* @var Smarty_Internal_Template $this */
  192. $template->tplFunctions = $this->tplFunctions;
  193. $template->inheritance = $this->inheritance;
  194. }
  195. /* @var Smarty_Internal_Template $parent */
  196. if (isset($parent->_objType) && ($parent->_objType === 2) && !empty($parent->tplFunctions)) {
  197. $template->tplFunctions = array_merge($parent->tplFunctions, $template->tplFunctions);
  198. }
  199. if ($function === 2) {
  200. if ($template->caching) {
  201. // return cache status of template
  202. if (!isset($template->cached)) {
  203. $template->loadCached();
  204. }
  205. $result = $template->cached->isCached($template);
  206. Smarty_Internal_Template::$isCacheTplObj[ $template->_getTemplateId() ] = $template;
  207. } else {
  208. return false;
  209. }
  210. } else {
  211. if ($saveVars) {
  212. $savedTplVars = $template->tpl_vars;
  213. $savedConfigVars = $template->config_vars;
  214. }
  215. ob_start();
  216. $template->_mergeVars();
  217. if (!empty(Smarty::$global_tpl_vars)) {
  218. $template->tpl_vars = array_merge(Smarty::$global_tpl_vars, $template->tpl_vars);
  219. }
  220. $result = $template->render(false, $function);
  221. $template->_cleanUp();
  222. if ($saveVars) {
  223. $template->tpl_vars = $savedTplVars;
  224. $template->config_vars = $savedConfigVars;
  225. } else {
  226. if (!$function && !isset(Smarty_Internal_Template::$tplObjCache[ $template->templateId ])) {
  227. $template->parent = null;
  228. $template->tpl_vars = $template->config_vars = array();
  229. Smarty_Internal_Template::$tplObjCache[ $template->templateId ] = $template;
  230. }
  231. }
  232. }
  233. if (isset($_smarty_old_error_level)) {
  234. error_reporting($_smarty_old_error_level);
  235. }
  236. return $result;
  237. }
  238. catch (Exception $e) {
  239. while (ob_get_level() > $level) {
  240. ob_end_clean();
  241. }
  242. if (isset($_smarty_old_error_level)) {
  243. error_reporting($_smarty_old_error_level);
  244. }
  245. throw $e;
  246. }
  247. }
  248. /**
  249. * Registers plugin to be used in templates
  250. *
  251. * @api Smarty::registerPlugin()
  252. * @link http://www.smarty.net/docs/en/api.register.plugin.tpl
  253. *
  254. * @param string $type plugin type
  255. * @param string $name name of template tag
  256. * @param callback $callback PHP callback to register
  257. * @param bool $cacheable if true (default) this function is cache able
  258. * @param mixed $cache_attr caching attributes if any
  259. *
  260. * @return \Smarty|\Smarty_Internal_Template
  261. * @throws SmartyException when the plugin tag is invalid
  262. */
  263. public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null)
  264. {
  265. return $this->ext->registerPlugin->registerPlugin($this, $type, $name, $callback, $cacheable, $cache_attr);
  266. }
  267. /**
  268. * load a filter of specified type and name
  269. *
  270. * @api Smarty::loadFilter()
  271. * @link http://www.smarty.net/docs/en/api.load.filter.tpl
  272. *
  273. * @param string $type filter type
  274. * @param string $name filter name
  275. *
  276. * @return bool
  277. * @throws SmartyException if filter could not be loaded
  278. */
  279. public function loadFilter($type, $name)
  280. {
  281. return $this->ext->loadFilter->loadFilter($this, $type, $name);
  282. }
  283. /**
  284. * Registers a filter function
  285. *
  286. * @api Smarty::registerFilter()
  287. * @link http://www.smarty.net/docs/en/api.register.filter.tpl
  288. *
  289. * @param string $type filter type
  290. * @param callback $callback
  291. * @param string|null $name optional filter name
  292. *
  293. * @return \Smarty|\Smarty_Internal_Template
  294. * @throws \SmartyException
  295. */
  296. public function registerFilter($type, $callback, $name = null)
  297. {
  298. return $this->ext->registerFilter->registerFilter($this, $type, $callback, $name);
  299. }
  300. /**
  301. * Registers object to be used in templates
  302. *
  303. * @api Smarty::registerObject()
  304. * @link http://www.smarty.net/docs/en/api.register.object.tpl
  305. *
  306. * @param string $object_name
  307. * @param object $object the referenced PHP object to register
  308. * @param array $allowed_methods_properties list of allowed methods (empty = all)
  309. * @param bool $format smarty argument format, else traditional
  310. * @param array $block_methods list of block-methods
  311. *
  312. * @return \Smarty|\Smarty_Internal_Template
  313. * @throws \SmartyException
  314. */
  315. public function registerObject($object_name, $object, $allowed_methods_properties = array(), $format = true,
  316. $block_methods = array())
  317. {
  318. return $this->ext->registerObject->registerObject($this, $object_name, $object, $allowed_methods_properties,
  319. $format, $block_methods);
  320. }
  321. /**
  322. * @param int $compile_check
  323. */
  324. public function setCompileCheck($compile_check)
  325. {
  326. $this->compile_check = (int)$compile_check;
  327. }
  328. /**
  329. * @param int $caching
  330. */
  331. public function setCaching($caching)
  332. {
  333. $this->caching = (int)$caching;
  334. }
  335. /**
  336. * @param int $cache_lifetime
  337. */
  338. public function setCacheLifetime($cache_lifetime)
  339. {
  340. $this->cache_lifetime = $cache_lifetime;
  341. }
  342. /**
  343. * @param string $compile_id
  344. */
  345. public function setCompileId($compile_id)
  346. {
  347. $this->compile_id = $compile_id;
  348. }
  349. /**
  350. * @param string $cache_id
  351. */
  352. public function setCacheId($cache_id)
  353. {
  354. $this->cache_id = $cache_id;
  355. }
  356. }