From fe5492c0e9c84c427356f1f5790c2edb9ec50d56 Mon Sep 17 00:00:00 2001 From: andrey Date: Mon, 25 Mar 2002 22:32:05 +0000 Subject: [PATCH] Initial implementation of block functions. --- Smarty.class.php | 1 + Smarty_Compiler.class.php | 85 ++++++++++++++++++++++++++++++++++ libs/Smarty.class.php | 1 + libs/Smarty_Compiler.class.php | 85 ++++++++++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) diff --git a/Smarty.class.php b/Smarty.class.php index 32c6eb4a..fd553e26 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -163,6 +163,7 @@ class Smarty var $_plugins = array( // table keeping track of plugins 'modifier' => array(), 'function' => array(), + 'block' => array(), 'compiler' => array(), 'prefilter' => array(), 'postfilter'=> array(), diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index 201bfc34..d429ed68 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -332,6 +332,8 @@ class Smarty_Compiler extends Smarty { default: if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) { return $output; + } else if ($this->_compile_block_tag($tag_command, $tag_args, $output)) { + return $output; } else { return $this->_compile_custom_tag($tag_command, $tag_args); } @@ -403,6 +405,89 @@ class Smarty_Compiler extends Smarty { } +/*======================================================================*\ + Function: _compile_block_tag + Purpose: compile block function tag +\*======================================================================*/ + function _compile_block_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; + + $plugin_file = SMARTY_DIR . + $this->plugins_dir . + '/block.' . + $tag_command . + '.php'; + + /* + * First we check if the block function has already been registered + * or loaded from a plugin file. + */ + if (isset($this->_plugins['block'][$tag_command])) { + $found = true; + $plugin_func = $this->_plugins['block'][$tag_command][0]; + if (!function_exists($plugin_func)) { + $message = "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 (file_exists($plugin_file) && is_readable($plugin_file)) { + $found = true; + + include_once $plugin_file; + + $plugin_func = 'smarty_block_' . $tag_command; + if (!function_exists($plugin_func)) { + $message = "plugin function $plugin_func() not found in $plugin_file\n"; + $have_function = false; + } else { + $this->_plugins['block'][$tag_command] = array($plugin_func, null, null); + } + } + + if (!$found) { + return false; + } else if (!$have_function) { + $this->_syntax_error($message, E_USER_WARNING); + return true; + } + + /* + * Even though we've located the plugin function, compilation + * happens only once, so the plugin will still need to be loaded + * at runtime for future requests. + */ + $this->_add_plugin('block', $tag_command); + + $arg_list = array(); + $attrs = $this->_parse_attrs($tag_args); + foreach ($attrs as $arg_name => $arg_value) { + if (is_bool($arg_value)) + $arg_value = $arg_value ? 'true' : 'false'; + $arg_list[] = "'$arg_name' => $arg_value"; + } + + if ($start_tag) { + $output = "_plugins['block']['$tag_command'][0](array(".implode(',', (array)$arg_list)."), null, \$this); ob_start(); ?>"; + } else { + $output = "_block = ob_get_contents(); ob_end_clean(); \$this->_plugins['block']['$tag_command'][0](array(".implode(',', (array)$arg_list)."), \$this->_block, \$this); ?>"; + } + + return true; + } + + /*======================================================================*\ Function: _compile_custom_tag Purpose: compile custom function tag diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 32c6eb4a..fd553e26 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -163,6 +163,7 @@ class Smarty var $_plugins = array( // table keeping track of plugins 'modifier' => array(), 'function' => array(), + 'block' => array(), 'compiler' => array(), 'prefilter' => array(), 'postfilter'=> array(), diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 201bfc34..d429ed68 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -332,6 +332,8 @@ class Smarty_Compiler extends Smarty { default: if ($this->_compile_compiler_tag($tag_command, $tag_args, $output)) { return $output; + } else if ($this->_compile_block_tag($tag_command, $tag_args, $output)) { + return $output; } else { return $this->_compile_custom_tag($tag_command, $tag_args); } @@ -403,6 +405,89 @@ class Smarty_Compiler extends Smarty { } +/*======================================================================*\ + Function: _compile_block_tag + Purpose: compile block function tag +\*======================================================================*/ + function _compile_block_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; + + $plugin_file = SMARTY_DIR . + $this->plugins_dir . + '/block.' . + $tag_command . + '.php'; + + /* + * First we check if the block function has already been registered + * or loaded from a plugin file. + */ + if (isset($this->_plugins['block'][$tag_command])) { + $found = true; + $plugin_func = $this->_plugins['block'][$tag_command][0]; + if (!function_exists($plugin_func)) { + $message = "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 (file_exists($plugin_file) && is_readable($plugin_file)) { + $found = true; + + include_once $plugin_file; + + $plugin_func = 'smarty_block_' . $tag_command; + if (!function_exists($plugin_func)) { + $message = "plugin function $plugin_func() not found in $plugin_file\n"; + $have_function = false; + } else { + $this->_plugins['block'][$tag_command] = array($plugin_func, null, null); + } + } + + if (!$found) { + return false; + } else if (!$have_function) { + $this->_syntax_error($message, E_USER_WARNING); + return true; + } + + /* + * Even though we've located the plugin function, compilation + * happens only once, so the plugin will still need to be loaded + * at runtime for future requests. + */ + $this->_add_plugin('block', $tag_command); + + $arg_list = array(); + $attrs = $this->_parse_attrs($tag_args); + foreach ($attrs as $arg_name => $arg_value) { + if (is_bool($arg_value)) + $arg_value = $arg_value ? 'true' : 'false'; + $arg_list[] = "'$arg_name' => $arg_value"; + } + + if ($start_tag) { + $output = "_plugins['block']['$tag_command'][0](array(".implode(',', (array)$arg_list)."), null, \$this); ob_start(); ?>"; + } else { + $output = "_block = ob_get_contents(); ob_end_clean(); \$this->_plugins['block']['$tag_command'][0](array(".implode(',', (array)$arg_list)."), \$this->_block, \$this); ?>"; + } + + return true; + } + + /*======================================================================*\ Function: _compile_custom_tag Purpose: compile custom function tag