3 * Smarty Internal Plugin Smarty Template Base
\r
4 * This file contains the basic shared methods for template handling
\r
7 * @subpackage Template
\r
12 * Class with shared template methods
\r
15 * @subpackage Template
\r
17 abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
\r
20 * fetches a rendered Smarty template
\r
22 * @param string $template the resource handle of the template file or template object
\r
23 * @param mixed $cache_id cache id to be used with this template
\r
24 * @param mixed $compile_id compile id to be used with this template
\r
25 * @param object $parent next higher level of Smarty variables
\r
26 * @param bool $display true: display, false: fetch
\r
27 * @param bool $merge_tpl_vars if true parent template variables merged in to local scope
\r
28 * @param bool $no_output_filter if true do not run output filter
\r
31 * @throws SmartyException
\r
32 * @return string rendered template output
\r
34 public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null, $display = false, $merge_tpl_vars = true, $no_output_filter = false)
\r
36 if ($template === null && $this instanceof $this->template_class) {
\r
39 if ($cache_id !== null && is_object($cache_id)) {
\r
40 $parent = $cache_id;
\r
43 if ($parent === null && ($this instanceof Smarty || is_string($template))) {
\r
46 // create template object if necessary
\r
47 $_template = ($template instanceof $this->template_class)
\r
49 : $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
\r
50 // if called by Smarty object make sure we use current caching status
\r
51 if ($this instanceof Smarty) {
\r
52 $_template->caching = $this->caching;
\r
54 // merge all variable scopes into template
\r
55 if ($merge_tpl_vars) {
\r
56 // save local variables
\r
57 $save_tpl_vars = $_template->tpl_vars;
\r
58 $save_config_vars = $_template->config_vars;
\r
59 $ptr_array = array($_template);
\r
61 while (isset($ptr->parent)) {
\r
62 $ptr_array[] = $ptr = $ptr->parent;
\r
64 $ptr_array = array_reverse($ptr_array);
\r
65 $parent_ptr = reset($ptr_array);
\r
66 $tpl_vars = $parent_ptr->tpl_vars;
\r
67 $config_vars = $parent_ptr->config_vars;
\r
68 while ($parent_ptr = next($ptr_array)) {
\r
69 if (!empty($parent_ptr->tpl_vars)) {
\r
70 $tpl_vars = array_merge($tpl_vars, $parent_ptr->tpl_vars);
\r
72 if (!empty($parent_ptr->config_vars)) {
\r
73 $config_vars = array_merge($config_vars, $parent_ptr->config_vars);
\r
76 if (!empty(Smarty::$global_tpl_vars)) {
\r
77 $tpl_vars = array_merge(Smarty::$global_tpl_vars, $tpl_vars);
\r
79 $_template->tpl_vars = $tpl_vars;
\r
80 $_template->config_vars = $config_vars;
\r
82 // dummy local smarty variable
\r
83 if (!isset($_template->tpl_vars['smarty'])) {
\r
84 $_template->tpl_vars['smarty'] = new Smarty_Variable;
\r
86 if (isset($this->smarty->error_reporting)) {
\r
87 $_smarty_old_error_level = error_reporting($this->smarty->error_reporting);
\r
89 // check URL debugging control
\r
90 if (!$this->smarty->debugging && $this->smarty->debugging_ctrl == 'URL') {
\r
91 if (isset($_SERVER['QUERY_STRING'])) {
\r
92 $_query_string = $_SERVER['QUERY_STRING'];
\r
94 $_query_string = '';
\r
96 if (false !== strpos($_query_string, $this->smarty->smarty_debug_id)) {
\r
97 if (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=on')) {
\r
98 // enable debugging for this browser session
\r
99 setcookie('SMARTY_DEBUG', true);
\r
100 $this->smarty->debugging = true;
\r
101 } elseif (false !== strpos($_query_string, $this->smarty->smarty_debug_id . '=off')) {
\r
102 // disable debugging for this browser session
\r
103 setcookie('SMARTY_DEBUG', false);
\r
104 $this->smarty->debugging = false;
\r
106 // enable debugging for this page
\r
107 $this->smarty->debugging = true;
\r
110 if (isset($_COOKIE['SMARTY_DEBUG'])) {
\r
111 $this->smarty->debugging = true;
\r
115 // must reset merge template date
\r
116 $_template->smarty->merged_templates_func = array();
\r
117 // get rendered template
\r
118 // disable caching for evaluated code
\r
119 if ($_template->source->recompiled) {
\r
120 $_template->caching = false;
\r
122 // checks if template exists
\r
123 if (!$_template->source->exists) {
\r
124 if ($_template->parent instanceof Smarty_Internal_Template) {
\r
125 $parent_resource = " in '{$_template->parent->template_resource}'";
\r
127 $parent_resource = '';
\r
129 throw new SmartyException("Unable to load template {$_template->source->type} '{$_template->source->name}'{$parent_resource}");
\r
131 // read from cache or render
\r
132 if (!($_template->caching == Smarty::CACHING_LIFETIME_CURRENT || $_template->caching == Smarty::CACHING_LIFETIME_SAVED) || !$_template->cached->valid) {
\r
133 // render template (not loaded and not in cache)
\r
134 if (!$_template->source->uncompiled) {
\r
135 /** @var Smarty_Internal_Template $_smarty_tpl
\r
136 * used in evaluated code
\r
138 $_smarty_tpl = $_template;
\r
139 if ($_template->source->recompiled) {
\r
140 $code = $_template->compiler->compileTemplate($_template);
\r
141 if ($this->smarty->debugging) {
\r
142 Smarty_Internal_Debug::start_render($_template);
\r
146 eval("?>" . $code);
\r
149 catch (Exception $e) {
\r
154 if (!$_template->compiled->exists || ($_template->smarty->force_compile && !$_template->compiled->isCompiled)) {
\r
155 $_template->compileTemplateSource();
\r
156 $code = file_get_contents($_template->compiled->filepath);
\r
157 eval("?>" . $code);
\r
159 $_template->compiled->loaded = true;
\r
160 $_template->compiled->isCompiled = true;
\r
162 if ($this->smarty->debugging) {
\r
163 Smarty_Internal_Debug::start_render($_template);
\r
165 if (!$_template->compiled->loaded) {
\r
166 include($_template->compiled->filepath);
\r
167 if ($_template->mustCompile) {
\r
168 // recompile and load again
\r
169 $_template->compileTemplateSource();
\r
170 $code = file_get_contents($_template->compiled->filepath);
\r
171 eval("?>" . $code);
\r
173 $_template->compiled->isCompiled = true;
\r
175 $_template->compiled->loaded = true;
\r
177 $_template->decodeProperties($_template->compiled->_properties, false);
\r
181 if (empty($_template->properties['unifunc']) || !is_callable($_template->properties['unifunc'])) {
\r
182 throw new SmartyException("Invalid compiled template for '{$_template->template_resource}'");
\r
184 array_unshift($_template->_capture_stack, array());
\r
186 // render compiled template
\r
188 $_template->properties['unifunc']($_template);
\r
189 // any unclosed {capture} tags ?
\r
190 if (isset($_template->_capture_stack[0][0])) {
\r
191 $_template->capture_error();
\r
193 array_shift($_template->_capture_stack);
\r
195 catch (Exception $e) {
\r
201 if ($_template->source->uncompiled) {
\r
202 if ($this->smarty->debugging) {
\r
203 Smarty_Internal_Debug::start_render($_template);
\r
207 $_template->source->renderUncompiled($_template);
\r
209 catch (Exception $e) {
\r
214 throw new SmartyException("Resource '$_template->source->type' must have 'renderUncompiled' method");
\r
217 $_output = ob_get_clean();
\r
218 if (!$_template->source->recompiled && empty($_template->properties['file_dependency'][$_template->source->uid])) {
\r
219 $_template->properties['file_dependency'][$_template->source->uid] = array($_template->source->filepath, $_template->source->timestamp, $_template->source->type);
\r
221 if ($_template->parent instanceof Smarty_Internal_Template) {
\r
222 $_template->parent->properties['file_dependency'] = array_merge($_template->parent->properties['file_dependency'], $_template->properties['file_dependency']);
\r
223 foreach ($_template->required_plugins as $code => $tmp1) {
\r
224 foreach ($tmp1 as $name => $tmp) {
\r
225 foreach ($tmp as $type => $data) {
\r
226 $_template->parent->required_plugins[$code][$name][$type] = $data;
\r
231 if ($this->smarty->debugging) {
\r
232 Smarty_Internal_Debug::end_render($_template);
\r
234 // write to cache when nessecary
\r
235 if (!$_template->source->recompiled && ($_template->caching == Smarty::CACHING_LIFETIME_SAVED || $_template->caching == Smarty::CACHING_LIFETIME_CURRENT)) {
\r
236 if ($this->smarty->debugging) {
\r
237 Smarty_Internal_Debug::start_cache($_template);
\r
239 $_template->properties['has_nocache_code'] = false;
\r
240 // get text between non-cached items
\r
241 $cache_split = preg_split("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output);
\r
242 // get non-cached items
\r
243 preg_match_all("!/\*%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*\/(.+?)/\*/%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!s", $_output, $cache_parts);
\r
245 // loop over items, stitch back together
\r
246 foreach ($cache_split as $curr_idx => $curr_split) {
\r
247 // escape PHP tags in template content
\r
248 $output .= preg_replace('/(<%|%>|<\?php|<\?|\?>)/', "<?php echo '\$1'; ?>\n", $curr_split);
\r
249 if (isset($cache_parts[0][$curr_idx])) {
\r
250 $_template->properties['has_nocache_code'] = true;
\r
251 // remove nocache tags from cache output
\r
252 $output .= preg_replace("!/\*/?%%SmartyNocache:{$_template->properties['nocache_hash']}%%\*/!", '', $cache_parts[0][$curr_idx]);
\r
255 if (!$no_output_filter && !$_template->has_nocache_code && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
\r
256 $output = Smarty_Internal_Filter_Handler::runFilter('output', $output, $_template);
\r
258 // rendering (must be done before writing cache file because of {function} nocache handling)
\r
259 /** @var Smarty_Internal_Template $_smarty_tpl
\r
260 * used in evaluated code
\r
262 $_smarty_tpl = $_template;
\r
265 eval("?>" . $output);
\r
266 $_output = ob_get_clean();
\r
268 catch (Exception $e) {
\r
272 // write cache file content
\r
273 $_template->writeCachedContent($output);
\r
274 if ($this->smarty->debugging) {
\r
275 Smarty_Internal_Debug::end_cache($_template);
\r
278 // var_dump('renderTemplate', $_template->has_nocache_code, $_template->template_resource, $_template->properties['nocache_hash'], $_template->parent->properties['nocache_hash'], $_output);
\r
279 if (!empty($_template->properties['nocache_hash']) && !empty($_template->parent->properties['nocache_hash'])) {
\r
280 // replace nocache_hash
\r
281 $_output = str_replace("{$_template->properties['nocache_hash']}", $_template->parent->properties['nocache_hash'], $_output);
\r
282 $_template->parent->has_nocache_code = $_template->parent->has_nocache_code || $_template->has_nocache_code;
\r
286 if ($this->smarty->debugging) {
\r
287 Smarty_Internal_Debug::start_cache($_template);
\r
291 array_unshift($_template->_capture_stack, array());
\r
293 // render cached template
\r
295 $_template->properties['unifunc']($_template);
\r
296 // any unclosed {capture} tags ?
\r
297 if (isset($_template->_capture_stack[0][0])) {
\r
298 $_template->capture_error();
\r
300 array_shift($_template->_capture_stack);
\r
301 $_output = ob_get_clean();
\r
303 catch (Exception $e) {
\r
307 if ($this->smarty->debugging) {
\r
308 Smarty_Internal_Debug::end_cache($_template);
\r
311 if ((!$this->caching || $_template->has_nocache_code || $_template->source->recompiled) && !$no_output_filter && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) {
\r
312 $_output = Smarty_Internal_Filter_Handler::runFilter('output', $_output, $_template);
\r
314 if (isset($this->error_reporting)) {
\r
315 error_reporting($_smarty_old_error_level);
\r
317 // display or fetch
\r
319 if ($this->caching && $this->cache_modified_check) {
\r
320 $_isCached = $_template->isCached() && !$_template->has_nocache_code;
\r
321 $_last_modified_date = @substr($_SERVER['HTTP_IF_MODIFIED_SINCE'], 0, strpos($_SERVER['HTTP_IF_MODIFIED_SINCE'], 'GMT') + 3);
\r
322 if ($_isCached && $_template->cached->timestamp <= strtotime($_last_modified_date)) {
\r
323 switch (PHP_SAPI) {
\r
324 case 'cgi': // php-cgi < 5.3
\r
325 case 'cgi-fcgi': // php-cgi >= 5.3
\r
326 case 'fpm-fcgi': // php-fpm >= 5.3.3
\r
327 header('Status: 304 Not Modified');
\r
331 if ( /* ^phpunit */
\r
332 !empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS']) /* phpunit$ */
\r
334 $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = '304 Not Modified';
\r
339 header($_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified');
\r
343 switch (PHP_SAPI) {
\r
345 if ( /* ^phpunit */
\r
346 !empty($_SERVER['SMARTY_PHPUNIT_DISABLE_HEADERS']) /* phpunit$ */
\r
348 $_SERVER['SMARTY_PHPUNIT_HEADERS'][] = 'Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT';
\r
353 header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_template->cached->timestamp) . ' GMT');
\r
362 if ($this->smarty->debugging) {
\r
363 Smarty_Internal_Debug::display_debug($_template);
\r
365 if ($merge_tpl_vars) {
\r
366 // restore local variables
\r
367 $_template->tpl_vars = $save_tpl_vars;
\r
368 $_template->config_vars = $save_config_vars;
\r
373 if ($merge_tpl_vars) {
\r
374 // restore local variables
\r
375 $_template->tpl_vars = $save_tpl_vars;
\r
376 $_template->config_vars = $save_config_vars;
\r
378 // return fetched content
\r
384 * displays a Smarty template
\r
386 * @param string $template the resource handle of the template file or template object
\r
387 * @param mixed $cache_id cache id to be used with this template
\r
388 * @param mixed $compile_id compile id to be used with this template
\r
389 * @param object $parent next higher level of Smarty variables
\r
391 public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
\r
393 // display template
\r
394 $this->fetch($template, $cache_id, $compile_id, $parent, true);
\r
398 * test if cache is valid
\r
400 * @param string|object $template the resource handle of the template file or template object
\r
401 * @param mixed $cache_id cache id to be used with this template
\r
402 * @param mixed $compile_id compile id to be used with this template
\r
403 * @param object $parent next higher level of Smarty variables
\r
405 * @return boolean cache status
\r
407 public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
\r
409 if ($template === null && $this instanceof $this->template_class) {
\r
410 return $this->cached->valid;
\r
412 if (!($template instanceof $this->template_class)) {
\r
413 if ($parent === null) {
\r
416 $template = $this->smarty->createTemplate($template, $cache_id, $compile_id, $parent, false);
\r
418 // return cache status of template
\r
419 return $template->cached->valid;
\r
423 * creates a data object
\r
425 * @param object $parent next higher level of Smarty variables
\r
427 * @returns Smarty_Data data object
\r
429 public function createData($parent = null)
\r
431 return new Smarty_Data($parent, $this);
\r
435 * Registers plugin to be used in templates
\r
437 * @param string $type plugin type
\r
438 * @param string $tag name of template tag
\r
439 * @param callback $callback PHP callback to register
\r
440 * @param boolean $cacheable if true (default) this fuction is cachable
\r
441 * @param array $cache_attr caching attributes if any
\r
443 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
444 * @throws SmartyException when the plugin tag is invalid
\r
446 public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null)
\r
448 if (isset($this->smarty->registered_plugins[$type][$tag])) {
\r
449 throw new SmartyException("Plugin tag \"{$tag}\" already registered");
\r
450 } elseif (!is_callable($callback)) {
\r
451 throw new SmartyException("Plugin \"{$tag}\" not callable");
\r
453 $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr);
\r
460 * Unregister Plugin
\r
462 * @param string $type of plugin
\r
463 * @param string $tag name of plugin
\r
465 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
467 public function unregisterPlugin($type, $tag)
\r
469 if (isset($this->smarty->registered_plugins[$type][$tag])) {
\r
470 unset($this->smarty->registered_plugins[$type][$tag]);
\r
477 * Registers a resource to fetch a template
\r
479 * @param string $type name of resource type
\r
480 * @param Smarty_Resource|array $callback or instance of Smarty_Resource, or array of callbacks to handle resource (deprecated)
\r
482 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
484 public function registerResource($type, $callback)
\r
486 $this->smarty->registered_resources[$type] = $callback instanceof Smarty_Resource ? $callback : array($callback, false);
\r
492 * Unregisters a resource
\r
494 * @param string $type name of resource type
\r
496 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
498 public function unregisterResource($type)
\r
500 if (isset($this->smarty->registered_resources[$type])) {
\r
501 unset($this->smarty->registered_resources[$type]);
\r
508 * Registers a cache resource to cache a template's output
\r
510 * @param string $type name of cache resource type
\r
511 * @param Smarty_CacheResource $callback instance of Smarty_CacheResource to handle output caching
\r
513 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
515 public function registerCacheResource($type, Smarty_CacheResource $callback)
\r
517 $this->smarty->registered_cache_resources[$type] = $callback;
\r
523 * Unregisters a cache resource
\r
525 * @param string $type name of cache resource type
\r
527 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
529 public function unregisterCacheResource($type)
\r
531 if (isset($this->smarty->registered_cache_resources[$type])) {
\r
532 unset($this->smarty->registered_cache_resources[$type]);
\r
539 * Registers object to be used in templates
\r
541 * @param $object_name
\r
542 * @param object $object_impl the referenced PHP object to register
\r
543 * @param array $allowed list of allowed methods (empty = all)
\r
544 * @param boolean $smarty_args smarty argument format, else traditional
\r
545 * @param array $block_methods list of block-methods
\r
547 * @throws SmartyException
\r
548 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
550 public function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
\r
552 // test if allowed methods callable
\r
553 if (!empty($allowed)) {
\r
554 foreach ((array) $allowed as $method) {
\r
555 if (!is_callable(array($object_impl, $method)) && !property_exists($object_impl, $method)) {
\r
556 throw new SmartyException("Undefined method or property '$method' in registered object");
\r
560 // test if block methods callable
\r
561 if (!empty($block_methods)) {
\r
562 foreach ((array) $block_methods as $method) {
\r
563 if (!is_callable(array($object_impl, $method))) {
\r
564 throw new SmartyException("Undefined method '$method' in registered object");
\r
568 // register the object
\r
569 $this->smarty->registered_objects[$object_name] =
\r
570 array($object_impl, (array) $allowed, (boolean) $smarty_args, (array) $block_methods);
\r
576 * return a reference to a registered object
\r
578 * @param string $name object name
\r
581 * @throws SmartyException if no such object is found
\r
583 public function getRegisteredObject($name)
\r
585 if (!isset($this->smarty->registered_objects[$name])) {
\r
586 throw new SmartyException("'$name' is not a registered object");
\r
588 if (!is_object($this->smarty->registered_objects[$name][0])) {
\r
589 throw new SmartyException("registered '$name' is not an object");
\r
592 return $this->smarty->registered_objects[$name][0];
\r
596 * unregister an object
\r
598 * @param string $name object name
\r
600 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
602 public function unregisterObject($name)
\r
604 if (isset($this->smarty->registered_objects[$name])) {
\r
605 unset($this->smarty->registered_objects[$name]);
\r
612 * Registers static classes to be used in templates
\r
614 * @param $class_name
\r
615 * @param string $class_impl the referenced PHP class to register
\r
617 * @throws SmartyException
\r
618 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
620 public function registerClass($class_name, $class_impl)
\r
623 if (!class_exists($class_impl)) {
\r
624 throw new SmartyException("Undefined class '$class_impl' in register template class");
\r
626 // register the class
\r
627 $this->smarty->registered_classes[$class_name] = $class_impl;
\r
633 * Registers a default plugin handler
\r
635 * @param callable $callback class/method name
\r
637 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
638 * @throws SmartyException if $callback is not callable
\r
640 public function registerDefaultPluginHandler($callback)
\r
642 if (is_callable($callback)) {
\r
643 $this->smarty->default_plugin_handler_func = $callback;
\r
645 throw new SmartyException("Default plugin handler '$callback' not callable");
\r
652 * Registers a default template handler
\r
654 * @param callable $callback class/method name
\r
656 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
657 * @throws SmartyException if $callback is not callable
\r
659 public function registerDefaultTemplateHandler($callback)
\r
661 if (is_callable($callback)) {
\r
662 $this->smarty->default_template_handler_func = $callback;
\r
664 throw new SmartyException("Default template handler '$callback' not callable");
\r
671 * Registers a default template handler
\r
673 * @param callable $callback class/method name
\r
675 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
676 * @throws SmartyException if $callback is not callable
\r
678 public function registerDefaultConfigHandler($callback)
\r
680 if (is_callable($callback)) {
\r
681 $this->smarty->default_config_handler_func = $callback;
\r
683 throw new SmartyException("Default config handler '$callback' not callable");
\r
690 * Registers a filter function
\r
692 * @param string $type filter type
\r
693 * @param callback $callback
\r
695 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
697 public function registerFilter($type, $callback)
\r
699 $this->smarty->registered_filters[$type][$this->_get_filter_name($callback)] = $callback;
\r
705 * Unregisters a filter function
\r
707 * @param string $type filter type
\r
708 * @param callback $callback
\r
710 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
712 public function unregisterFilter($type, $callback)
\r
714 $name = $this->_get_filter_name($callback);
\r
715 if (isset($this->smarty->registered_filters[$type][$name])) {
\r
716 unset($this->smarty->registered_filters[$type][$name]);
\r
723 * Return internal filter name
\r
725 * @param callback $function_name
\r
727 * @return string internal filter name
\r
729 public function _get_filter_name($function_name)
\r
731 if (is_array($function_name)) {
\r
732 $_class_name = (is_object($function_name[0]) ?
\r
733 get_class($function_name[0]) : $function_name[0]);
\r
735 return $_class_name . '_' . $function_name[1];
\r
737 return $function_name;
\r
742 * load a filter of specified type and name
\r
744 * @param string $type filter type
\r
745 * @param string $name filter name
\r
747 * @throws SmartyException if filter could not be loaded
\r
749 public function loadFilter($type, $name)
\r
751 $_plugin = "smarty_{$type}filter_{$name}";
\r
752 $_filter_name = $_plugin;
\r
753 if ($this->smarty->loadPlugin($_plugin)) {
\r
754 if (class_exists($_plugin, false)) {
\r
755 $_plugin = array($_plugin, 'execute');
\r
757 if (is_callable($_plugin)) {
\r
758 $this->smarty->registered_filters[$type][$_filter_name] = $_plugin;
\r
763 throw new SmartyException("{$type}filter \"{$name}\" not callable");
\r
767 * unload a filter of specified type and name
\r
769 * @param string $type filter type
\r
770 * @param string $name filter name
\r
772 * @return Smarty_Internal_Templatebase current Smarty_Internal_Templatebase (or Smarty or Smarty_Internal_Template) instance for chaining
\r
774 public function unloadFilter($type, $name)
\r
776 $_filter_name = "smarty_{$type}filter_{$name}";
\r
777 if (isset($this->smarty->registered_filters[$type][$_filter_name])) {
\r
778 unset ($this->smarty->registered_filters[$type][$_filter_name]);
\r
785 * preg_replace callback to convert camelcase getter/setter to underscore property names
\r
787 * @param string $match match string
\r
789 * @return string replacemant
\r
791 private function replaceCamelcase($match)
\r
793 return "_" . strtolower($match[1]);
\r
797 * Handle unknown class methods
\r
799 * @param string $name unknown method-name
\r
800 * @param array $args argument array
\r
802 * @throws SmartyException
\r
804 public function __call($name, $args)
\r
806 static $_prefixes = array('set' => true, 'get' => true);
\r
807 static $_resolved_property_name = array();
\r
808 static $_resolved_property_source = array();
\r
810 // method of Smarty object?
\r
811 if (method_exists($this->smarty, $name)) {
\r
812 return call_user_func_array(array($this->smarty, $name), $args);
\r
814 // see if this is a set/get for a property
\r
815 $first3 = strtolower(substr($name, 0, 3));
\r
816 if (isset($_prefixes[$first3]) && isset($name[3]) && $name[3] !== '_') {
\r
817 if (isset($_resolved_property_name[$name])) {
\r
818 $property_name = $_resolved_property_name[$name];
\r
820 // try to keep case correct for future PHP 6.0 case-sensitive class methods
\r
821 // lcfirst() not available < PHP 5.3.0, so improvise
\r
822 $property_name = strtolower(substr($name, 3, 1)) . substr($name, 4);
\r
823 // convert camel case to underscored name
\r
824 $property_name = preg_replace_callback('/([A-Z])/', array($this, 'replaceCamelcase'), $property_name);
\r
825 $_resolved_property_name[$name] = $property_name;
\r
827 if (isset($_resolved_property_source[$property_name])) {
\r
828 $_is_this = $_resolved_property_source[$property_name];
\r
831 if (property_exists($this, $property_name)) {
\r
833 } elseif (property_exists($this->smarty, $property_name)) {
\r
836 $_resolved_property_source[$property_name] = $_is_this;
\r
839 if ($first3 == 'get') {
\r
840 return $this->$property_name;
\r
842 return $this->$property_name = $args[0];
\r
844 } elseif ($_is_this === false) {
\r
845 if ($first3 == 'get') {
\r
846 return $this->smarty->$property_name;
\r
848 return $this->smarty->$property_name = $args[0];
\r
851 throw new SmartyException("property '$property_name' does not exist.");
\r
854 if ($name == 'Smarty') {
\r
855 throw new SmartyException("PHP5 requires you to call __construct() instead of Smarty()");
\r
858 throw new SmartyException("Call of unknown method '$name'.");
\r