3 * Smarty Internal Plugin Smarty Template Base
4 * This file contains the basic shared methods for template handling
12 * Class with shared smarty/template methods
15 * @subpackage Template
17 * @property int $_objType
19 * The following methods will be dynamically loaded by the extension handler when they are called.
20 * They are located in a corresponding Smarty_Internal_Method_xxxx class
22 * @method Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
23 * @method Smarty_Internal_TemplateBase addDefaultModifier(mixed $modifiers)
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 getTags(mixed $template = null)
29 * @method object getRegisteredObject(string $object_name)
30 * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
31 * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
32 * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
33 * @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
34 * @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
35 * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
36 * @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
37 * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
38 * @method Smarty_Internal_TemplateBase setDefaultModifier(mixed $modifiers)
39 * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
40 * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
41 * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
42 * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
43 * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
44 * @method Smarty_Internal_TemplateBase unregisterResource(string $name)
45 * @method Smarty _getSmartyObj()
47 abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
50 * Set this if you want different sets of cache files for the same
55 public $cache_id = null;
58 * Set this if you want different sets of compiled files for the same
63 public $compile_id = null;
70 public $caching = false;
73 * cache lifetime in seconds
77 public $cache_lifetime = 3600;
80 * Array of source information for known template functions
84 public $tplFunctions = array();
91 public $_cache = array();
94 * fetches a rendered Smarty template
96 * @param string $template the resource handle of the template file or template object
97 * @param mixed $cache_id cache id to be used with this template
98 * @param mixed $compile_id compile id to be used with this template
99 * @param object $parent next higher level of Smarty variables
102 * @throws SmartyException
103 * @return string rendered template output
105 public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
107 $result = $this->_execute($template, $cache_id, $compile_id, $parent, 0);
108 return $result === null ? ob_get_clean() : $result;
112 * displays a Smarty template
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
119 public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
122 $this->_execute($template, $cache_id, $compile_id, $parent, 1);
126 * test if cache is valid
128 * @api Smarty::isCached()
129 * @link http://www.smarty.net/docs/en/api.is.cached.tpl
131 * @param null|string|\Smarty_Internal_Template $template the resource handle of the template file or template object
132 * @param mixed $cache_id cache id to be used with this template
133 * @param mixed $compile_id compile id to be used with this template
134 * @param object $parent next higher level of Smarty variables
136 * @return boolean cache status
138 public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
140 return $this->_execute($template, $cache_id, $compile_id, $parent, 2);
144 * fetches a rendered Smarty template
146 * @param string $template the resource handle of the template file or template object
147 * @param mixed $cache_id cache id to be used with this template
148 * @param mixed $compile_id compile id to be used with this template
149 * @param object $parent next higher level of Smarty variables
150 * @param string $function function type 0 = fetch, 1 = display, 2 = isCache
154 * @throws \SmartyException
156 private function _execute($template, $cache_id, $compile_id, $parent, $function)
158 $smarty = $this->_getSmartyObj();
160 if ($template === null) {
161 if (!$this->_isTplObj()) {
162 throw new SmartyException($function . '():Missing \'$template\' parameter');
166 } elseif (is_object($template)) {
167 /* @var Smarty_Internal_Template $template */
168 if (!isset($template->_objType) || !$template->_isTplObj()) {
169 throw new SmartyException($function . '():Template object expected');
172 // get template object
175 $template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent ? $parent : $this, false);
176 if ($this->_objType == 1) {
177 // set caching in template object
178 $template->caching = $this->caching;
181 // fetch template content
182 $level = ob_get_level();
184 $_smarty_old_error_level =
185 isset($smarty->error_reporting) ? error_reporting($smarty->error_reporting) : null;
186 if ($this->_objType == 2) {
187 /* @var Smarty_Internal_Template $this */
188 $template->tplFunctions = $this->tplFunctions;
189 $template->inheritance = $this->inheritance;
191 /* @var Smarty_Internal_Template $parent */
192 if (isset($parent->_objType) && ($parent->_objType == 2) && !empty($parent->tplFunctions)) {
193 $template->tplFunctions = array_merge($parent->tplFunctions, $template->tplFunctions);
195 if ($function == 2) {
196 if ($template->caching) {
197 // return cache status of template
198 if (!isset($template->cached)) {
199 $template->loadCached();
201 $result = $template->cached->isCached($template);
202 Smarty_Internal_Template::$isCacheTplObj[ $template->_getTemplateId() ] = $template;
208 $savedTplVars = $template->tpl_vars;
209 $savedConfigVars = $template->config_vars;
212 $template->_mergeVars();
213 if (!empty(Smarty::$global_tpl_vars)) {
214 $template->tpl_vars = array_merge(Smarty::$global_tpl_vars, $template->tpl_vars);
216 $result = $template->render(false, $function);
217 $template->_cleanUp();
219 $template->tpl_vars = $savedTplVars;
220 $template->config_vars = $savedConfigVars;
222 if (!$function && !isset(Smarty_Internal_Template::$tplObjCache[ $template->templateId ])) {
223 $template->parent = null;
224 $template->tpl_vars = $template->config_vars = array();
225 Smarty_Internal_Template::$tplObjCache[ $template->templateId ] = $template;
229 if (isset($_smarty_old_error_level)) {
230 error_reporting($_smarty_old_error_level);
234 catch (Exception $e) {
235 while (ob_get_level() > $level) {
238 if (isset($_smarty_old_error_level)) {
239 error_reporting($_smarty_old_error_level);
246 * Registers plugin to be used in templates
248 * @api Smarty::registerPlugin()
249 * @link http://www.smarty.net/docs/en/api.register.plugin.tpl
251 * @param string $type plugin type
252 * @param string $name name of template tag
253 * @param callback $callback PHP callback to register
254 * @param bool $cacheable if true (default) this function is cache able
255 * @param mixed $cache_attr caching attributes if any
257 * @return \Smarty|\Smarty_Internal_Template
258 * @throws SmartyException when the plugin tag is invalid
260 public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null)
262 return $this->ext->registerPlugin->registerPlugin($this, $type, $name, $callback, $cacheable, $cache_attr);
266 * load a filter of specified type and name
268 * @api Smarty::loadFilter()
269 * @link http://www.smarty.net/docs/en/api.load.filter.tpl
271 * @param string $type filter type
272 * @param string $name filter name
275 * @throws SmartyException if filter could not be loaded
277 public function loadFilter($type, $name)
279 return $this->ext->loadFilter->loadFilter($this, $type, $name);
283 * Registers a filter function
285 * @api Smarty::registerFilter()
286 * @link http://www.smarty.net/docs/en/api.register.filter.tpl
288 * @param string $type filter type
289 * @param callback $callback
290 * @param string|null $name optional filter name
292 * @return \Smarty|\Smarty_Internal_Template
293 * @throws \SmartyException
295 public function registerFilter($type, $callback, $name = null)
297 return $this->ext->registerFilter->registerFilter($this, $type, $callback, $name);
301 * Registers object to be used in templates
303 * @api Smarty::registerObject()
304 * @link http://www.smarty.net/docs/en/api.register.object.tpl
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
312 * @return \Smarty|\Smarty_Internal_Template
313 * @throws \SmartyException
315 public function registerObject($object_name, $object, $allowed_methods_properties = array(), $format = true,
316 $block_methods = array())
318 return $this->ext->registerObject->registerObject($this, $object_name, $object, $allowed_methods_properties,
319 $format, $block_methods);
323 * @param boolean $caching
325 public function setCaching($caching)
327 $this->caching = $caching;
331 * @param int $cache_lifetime
333 public function setCacheLifetime($cache_lifetime)
335 $this->cache_lifetime = $cache_lifetime;
339 * @param string $compile_id
341 public function setCompileId($compile_id)
343 $this->compile_id = $compile_id;
347 * @param string $cache_id
349 public function setCacheId($cache_id)
351 $this->cache_id = $cache_id;