smarty_internal_templatebase.php 13 KB

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