4 * Smarty Internal Plugin Register
6 * External Smarty methods register/unregister
13 * Class for register/unregister methods
15 class Smarty_Internal_Register {
17 function __construct($smarty)
19 $this->smarty = $smarty;
22 * Registers plugin to be used in templates
24 * @param string $type plugin type
25 * @param string $tag name of template tag
26 * @param callback $callback PHP callback to register
27 * @param boolean $cacheable if true (default) this fuction is cachable
28 * @param array $cache_attr caching attributes if any
31 public function registerPlugin($type, $tag, $callback, $cacheable = true, $cache_attr = null)
33 if (isset($this->smarty->registered_plugins[$type][$tag])) {
34 throw new Exception("Plugin tag \"{$tag}\" already registered");
35 } elseif (!is_callable($callback)) {
36 throw new Exception("Plugin \"{$tag}\" not callable");
38 $this->smarty->registered_plugins[$type][$tag] = array($callback, (bool) $cacheable, (array) $cache_attr);
45 * @param string $type of plugin
46 * @param string $tag name of plugin
48 function unregisterPlugin($type, $tag)
50 if (isset($this->smarty->registered_plugins[$type][$tag])) {
51 unset($this->smarty->registered_plugins[$type][$tag]);
56 * Registers a resource to fetch a template
58 * @param string $type name of resource type
59 * @param array $callback array of callbacks to handle resource
61 public function registerResource($type, $callback)
63 $this->smarty->registered_resources[$type] = array($callback, false);
67 * Unregisters a resource
69 * @param string $type name of resource type
71 function unregisterResource($type)
73 if (isset($this->smarty->registered_resources[$type])) {
74 unset($this->smarty->registered_resources[$type]);
80 * Registers object to be used in templates
82 * @param string $object name of template object
83 * @param object $ &$object_impl the referenced PHP object to register
84 * @param mixed $ null | array $allowed list of allowed methods (empty = all)
85 * @param boolean $smarty_args smarty argument format, else traditional
86 * @param mixed $ null | array $block_functs list of methods that are block format
88 function registerObject($object_name, $object_impl, $allowed = array(), $smarty_args = true, $block_methods = array())
90 // test if allowed methodes callable
91 if (!empty($allowed)) {
92 foreach ((array)$allowed as $method) {
93 if (!is_callable(array($object_impl, $method))) {
94 throw new SmartyException("Undefined method '$method' in registered object");
98 // test if block methodes callable
99 if (!empty($block_methods)) {
100 foreach ((array)$block_methods as $method) {
101 if (!is_callable(array($object_impl, $method))) {
102 throw new SmartyException("Undefined method '$method' in registered object");
106 // register the object
107 $this->smarty->registered_objects[$object_name] =
108 array($object_impl, (array)$allowed, (boolean)$smarty_args, (array)$block_methods);
112 * Registers static classes to be used in templates
114 * @param string $class name of template class
115 * @param string $class_impl the referenced PHP class to register
117 function registerClass($class_name, $class_impl)
120 if (!class_exists($class_impl)) {
121 throw new SmartyException("Undefined class '$class_impl' in register template class");
123 // register the class
124 $this->smarty->registered_classes[$class_name] = $class_impl;
128 * Registers a default plugin handler
130 * @param $callback mixed string | array $plugin class/methode name
132 function registerDefaultPluginHandler($callback)
134 if (is_callable($callback)) {
135 $this->smarty->default_plugin_handler_func = $callback;
137 throw new SmartyException("Default plugin handler '$callback' not callable");
142 * Registers a default template handler
144 * @param $callback mixed string | array class/method name
146 function registerDefaultTemplateHandler($callback)
148 if (is_callable($callback)) {
149 $this->smarty->default_template_handler_func = $callback;
151 throw new SmartyException("Default template handler '$callback' not callable");