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 11KB

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