smarty_internal_templatebase.php 12 KB

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