compilerblock plugins patch

From: Date: Mon, 02 Dec 2002 11:47:10 +0000
Subject: compilerblock plugins patch
Groups: php.smarty.dev 
Request: Send a blank email to php-smarty-dev+get-356@lists.php.net to get a copy of this message
hello, i patched smarty to support block-function at compile time. the compilerblock-function is searched as compilerblock.[name].php in the plugins_dirs and has precedence before simple (==old) compiler-funcions. alternatively you can register compilerblock-functions using $smarty->register_compilerblock_function(..). the function has to be declared as: function func($tag_args, $start_tag, &$smarty) ... or function smarty_compilerblock_csv($tag_args, $start_tag, &$smarty) ... where tag_args ar the (unparsed) arguments, like in the simple compiler-functions and $start_tag is a boolean indicating wether the function was called at the beginning or the end of the block. i attached the diff against the lates cvs-version. it's not small, but pretty simple, since it is a mainly a rip-off of Smarty_Compiler::_compile_block_tag() i would love to see it applied in Smarty-2.3.2 and i am willing to provide documentation for such plugins and to maintain this patch if necessary. any use for it? any comments? regards messju mohr

Common subdirectories: ../Smarty-cvs-Ref/smarty/CVS and ./CVS diff -p1 ../Smarty-cvs-Ref/smarty/Smarty.class.php ./Smarty.class.php *** ../Smarty-cvs-Ref/smarty/Smarty.class.php Thu Nov 21 16:12:12 2002 --- ./Smarty.class.php Mon Dec 2 12:28:20 2002 *************** class Smarty *** 185,186 **** --- 185,187 ---- 'compiler' => array(), + 'compilerblock' => array(), 'prefilter' => array(), *************** class Smarty *** 365,366 **** --- 366,386 ---- } + + /*======================================================================*\ + Function: register_compilerblock_function + Purpose: Registers compiler block function + \*======================================================================*/ + function register_compilerblock_function($function, $function_impl) + { + $this->_plugins['compilerblock'][$function] = + array($function_impl, null, null, false); + } + + /*======================================================================*\ + Function: unregister_compilerblock_function + Purpose: Unregisters compiler block function + \*======================================================================*/ + function unregister_compilerblock_function($function) + { + unset($this->_plugins['compilerblock'][$function]); + } diff -p1 ../Smarty-cvs-Ref/smarty/Smarty_Compiler.class.php ./Smarty_Compiler.class.php *** ../Smarty-cvs-Ref/smarty/Smarty_Compiler.class.php Fri Nov 22 15:33:03 2002 --- ./Smarty_Compiler.class.php Mon Dec 2 11:25:23 2002 *************** class Smarty_Compiler extends Smarty { *** 330,332 **** default: ! if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) { return $output; --- 330,334 ---- default: ! if ($this->_compile_compilerblock_tag($tag_command, $tag_args, $output)) { ! return $output; ! } else if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) { return $output; *************** class Smarty_Compiler extends Smarty { *** 341,342 **** --- 343,408 ---- + /*======================================================================*\ + Function: _compile_compilerblock_tag + Purpose: compile compilerblock function tag + \*======================================================================*/ + function _compile_compilerblock_tag($tag_command, $tag_args, &$output) + { + if ($tag_command{0} == '/') { + $start_tag = false; + $tag_command = substr($tag_command, 1); + } else + $start_tag = true; + + $found = false; + $have_function = true; + + /* + * First we check if the block function has already been registered + * or loaded from a plugin file. + */ + if (isset($this->_plugins['compilerblock'][$tag_command])) { + $found = true; + $plugin_func = $this->_plugins['compilerblock'][$tag_command][0]; + if (!function_exists($plugin_func)) { + $message = "compiler block function '$tag_command' is not implemented"; + $have_function = false; + } + } + /* + * Otherwise we need to load plugin file and look for the function + * inside it. + */ + else if ($plugin_file = $this->_get_plugin_filepath('compilerblock', $tag_command)) { + $found = true; + + include_once $plugin_file; + + $plugin_func = 'smarty_compilerblock_' . $tag_command; + if (!function_exists($plugin_func)) { + $message = "plugin function $plugin_func() not found in $plugin_file\n"; + $have_function = false; + } else { + $this->_plugins['compilerblock'][$tag_command] = array($plugin_func, null, null); + } + } + + if (!$found) { + return false; + } else if (!$have_function) { + $this->_syntax_error($message, E_USER_WARNING); + return true; + } + + if ($start_tag) { + $this->_tag_stack[] = array($tag_command, $tag_args); + } else { + $tag = array_pop($this->_tag_stack); + $tag_args = $tag[1]; + } + + $output = "<?php ".$this->_plugins['compilerblock'][$tag_command][0]($tag_args, $start_tag, $this)." ?>"; + + return true; + } + /*======================================================================*\ Common subdirectories: ../Smarty-cvs-Ref/smarty/configs and ./configs Common subdirectories: ../Smarty-cvs-Ref/smarty/docs and ./docs Common subdirectories: ../Smarty-cvs-Ref/smarty/misc and ./misc Common subdirectories: ../Smarty-cvs-Ref/smarty/plugins and ./plugins Common subdirectories: ../Smarty-cvs-Ref/smarty/templates and ./templates
« previous php.smarty.dev (#356) next »