From 9aaa64f3880476742f8468e3ccc24996aa94d442 Mon Sep 17 00:00:00 2001 From: andrey Date: Tue, 26 Mar 2002 21:01:52 +0000 Subject: [PATCH] Block functions changes. --- CREDITS | 4 ++-- NEWS | 1 + Smarty.class.php | 20 ++++++++++++++++++++ Smarty_Compiler.class.php | 20 ++++++++++---------- TODO | 1 - libs/Smarty.class.php | 20 ++++++++++++++++++++ libs/Smarty_Compiler.class.php | 20 ++++++++++---------- 7 files changed, 63 insertions(+), 23 deletions(-) diff --git a/CREDITS b/CREDITS index e644203e..a844961f 100644 --- a/CREDITS +++ b/CREDITS @@ -3,8 +3,8 @@ Monte Ohrt : concept" implementation, and maintains documentation & code base. Andrei Zmievski : - Rewrote parser from scratch, developed plugin architecture, and maintains - code base. + Rewrote parser from scratch, developed plugin architecture, reorganized + documentation, and maintains code base. Anne Holz : Provided creative input with respect to web design. diff --git a/NEWS b/NEWS index c7395f15..99f8a2ec 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,4 @@ + - implemented support for block functions. (Andrei) - made it possible to assign variables in pre/postfilter plugins. (Andrei) diff --git a/Smarty.class.php b/Smarty.class.php index fd553e26..5237f081 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -150,6 +150,7 @@ class Smarty var $_smarty_vars = null; // stores run-time $smarty.* vars var $_sections = array(); // keeps track of sections var $_foreach = array(); // keeps track of foreach blocks + var $_tag_stack = array(); // keeps track of tag hierarchy var $_conf_obj = null; // configuration object var $_config = array(); // loaded configuration settings var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' @@ -275,6 +276,25 @@ class Smarty unset($this->_plugins['function'][$function]); } +/*======================================================================*\ + Function: register_block + Purpose: Registers block function to be used in templates +\*======================================================================*/ + function register_block($block, $block_impl) + { + $this->_plugins['block'][$block] = + array($block_impl, null, null, false); + } + +/*======================================================================*\ + Function: unregister_block + Purpose: Unregisters block function +\*======================================================================*/ + function unregister_block($block) + { + unset($this->_plugins['block'][$block]); + } + /*======================================================================*\ Function: register_compiler_function Purpose: Registers compiler function diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index d429ed68..0b55cf95 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -470,18 +470,18 @@ class Smarty_Compiler extends Smarty { */ $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(); ?>"; + $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"; + } + + $output = "_tag_stack[] = array('$tag_command', array(".implode(',', (array)$arg_list).")); \$this->_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); ?>"; + $output = "_block_content = ob_get_contents(); ob_end_clean(); \$this->_plugins['block']['$tag_command'][0](\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_block_content, \$this); array_pop(\$this->_tag_stack); ?>"; } return true; diff --git a/TODO b/TODO index 9e1ed625..e6893166 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,6 @@ * support implementations of prefiltes, mods, and others as class methods. * possibly implement default modifiers that apply to variables upon display * think about possibility of supporting something like {$data[foo].$key[bar]} -* allow {custom} .. {/custom} style of custom functions * ability to concatenate values/strings together * fix all E_NOTICE warnings * make simple math easier diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index fd553e26..5237f081 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -150,6 +150,7 @@ class Smarty var $_smarty_vars = null; // stores run-time $smarty.* vars var $_sections = array(); // keeps track of sections var $_foreach = array(); // keeps track of foreach blocks + var $_tag_stack = array(); // keeps track of tag hierarchy var $_conf_obj = null; // configuration object var $_config = array(); // loaded configuration settings var $_smarty_md5 = 'f8d698aea36fcbead2b9d5359ffca76f'; // md5 checksum of the string 'Smarty' @@ -275,6 +276,25 @@ class Smarty unset($this->_plugins['function'][$function]); } +/*======================================================================*\ + Function: register_block + Purpose: Registers block function to be used in templates +\*======================================================================*/ + function register_block($block, $block_impl) + { + $this->_plugins['block'][$block] = + array($block_impl, null, null, false); + } + +/*======================================================================*\ + Function: unregister_block + Purpose: Unregisters block function +\*======================================================================*/ + function unregister_block($block) + { + unset($this->_plugins['block'][$block]); + } + /*======================================================================*\ Function: register_compiler_function Purpose: Registers compiler function diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index d429ed68..0b55cf95 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -470,18 +470,18 @@ class Smarty_Compiler extends Smarty { */ $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(); ?>"; + $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"; + } + + $output = "_tag_stack[] = array('$tag_command', array(".implode(',', (array)$arg_list).")); \$this->_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); ?>"; + $output = "_block_content = ob_get_contents(); ob_end_clean(); \$this->_plugins['block']['$tag_command'][0](\$this->_tag_stack[count(\$this->_tag_stack)-1][1], \$this->_block_content, \$this); array_pop(\$this->_tag_stack); ?>"; } return true;