From 5b4b13a319dcbc1fc696a1c4995140fe8b859149 Mon Sep 17 00:00:00 2001 From: messju Date: Sun, 8 Jun 2003 01:07:39 +0000 Subject: [PATCH] added block-methods for registered objects --- NEWS | 1 + libs/Smarty.class.php | 5 ++- libs/Smarty_Compiler.class.php | 72 +++++++++++++++++++++++++--------- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/NEWS b/NEWS index 5be4f1b8..478ca0b0 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,4 @@ + - added block-methods for registered objects (Bharat Mediratta, messju) - ignore one char resource names like c:foo.tpl (Monte) - added default_resource_type feature (Monte) - fix bug where config file starts with hidden section (boots, Monte) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 5185981e..f82d9f43 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -721,13 +721,14 @@ class Smarty * @param object &$object_impl the referenced PHP object to register * @param null|array $allowed list of allowed methods (empty = all) * @param boolean $smarty_args smarty argument format, else traditional + * @param null|array $block_functs list of methods that are block format */ - function register_object($object, &$object_impl, $allowed = array(), $smarty_args = true) + function register_object($object, &$object_impl, $allowed = array(), $smarty_args = true, $block_methods = array()) { settype($allowed, 'array'); settype($smarty_args, 'boolean'); $this->_reg_objects[$object] = - array(&$object_impl, $allowed, $smarty_args); + array(&$object_impl, $allowed, $smarty_args, $block_methods); } /** diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 43c48785..9eea3261 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -379,7 +379,7 @@ class Smarty_Compiler extends Smarty { /* Split tag into two three parts: command, command modifiers and the arguments. */ if(! preg_match('/^(?:(' . $this->_obj_call_regexp . '|' . $this->_var_regexp - . '|' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*)) + . '|\/?' . $this->_reg_obj_regexp . '|\/?' . $this->_func_regexp . ')(' . $this->_mod_regexp . '*)) (?:\s+(.*))?$ /xs', $template_tag, $match)) { $this->_syntax_error("unrecognized tag: $template_tag", E_USER_ERROR, __FILE__, __LINE__); @@ -401,7 +401,7 @@ class Smarty_Compiler extends Smarty { } /* If the tag name is a registered object, we process it. */ - if (preg_match('!^' . $this->_reg_obj_regexp . '$!', $tag_command)) { + if (preg_match('!^\/?' . $this->_reg_obj_regexp . '$!', $tag_command)) { return $this->_compile_registered_object_tag($tag_command, $this->_parse_attrs($tag_args), $tag_modifier); } @@ -691,6 +691,13 @@ class Smarty_Compiler extends Smarty { */ function _compile_registered_object_tag($tag_command, $attrs, $tag_modifier) { + if ($tag_command{0} == '/') { + $start_tag = false; + $tag_command = substr($tag_command, 1); + } else { + $start_tag = true; + } + list($object, $obj_comp) = explode('->', $tag_command); $arg_list = array(); @@ -708,36 +715,63 @@ class Smarty_Compiler extends Smarty { } } + if($this->_reg_objects[$object][2]) { + // smarty object argument format + $args = "array(".implode(',', (array)$arg_list)."), \$this"; + } else { + // traditional argument format + $args = implode(',', array_values($attrs)); + if (empty($args)) { + $args = 'null'; + } + } + + $prefix = ''; + $postfix = ''; if(!is_object($this->_reg_objects[$object][0])) { $this->_trigger_fatal_error("registered '$object' is not an object"); } elseif(!empty($this->_reg_objects[$object][1]) && !in_array($obj_comp, $this->_reg_objects[$object][1])) { $this->_trigger_fatal_error("'$obj_comp' is not a registered component of object '$object'"); } elseif(method_exists($this->_reg_objects[$object][0], $obj_comp)) { // method - if($this->_reg_objects[$object][2]) { - // smarty object argument format - $return = "\$this->_reg_objects['$object'][0]->$obj_comp(array(".implode(',', (array)$arg_list)."), \$this)"; + if(in_array($obj_comp, $this->_reg_objects[$object][3])) { + // block method + if ($start_tag) { + $prefix = "\$this->_tag_stack[] = array('$obj_comp', $args); "; + $prefix .= "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], null, \$this, \$_block_repeat=true); "; + $prefix .= "while (\$_block_repeat) { ob_start();"; + $return = null; + $postfix = ''; } else { - // traditional argument format - $return = "\$this->_reg_objects['$object'][0]->$obj_comp(".implode(',', array_values($attrs)).")"; + $prefix = "\$this->_obj_block_content = ob_get_contents(); ob_end_clean(); "; + $return = "\$this->_reg_objects['$object'][0]->$obj_comp(\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_obj_block_content, \$this, \$_block_repeat=false)"; + $postfix = "} array_pop(\$this->_tag_stack);"; + } + } else { + // non-block method + $return = "\$this->_reg_objects['$object'][0]->$obj_comp($args)"; } } else { // property $return = "\$this->_reg_objects['$object'][0]->$obj_comp"; } - - if($tag_modifier != '') { - $this->_parse_modifiers($return, $tag_modifier); - } - - if($_assign_var) { - return "assign('" . $this->_dequote($_assign_var) ."', $return); ?>\n"; - } else { - return '\n"; - } + + if($return != null) { + if($tag_modifier != '') { + $this->_parse_modifiers($return, $tag_modifier); + } + + if(!empty($_assign_var)) { + $output = "\$this->assign('" . $this->_dequote($_assign_var) ."', $return);"; + } else { + $output = 'echo ' . $return . ';'; + } + } else { + $output = ''; + } + + return '\n"; } - - /** * Compile {insert ...} tag