From f15be25a421a0884cd3143c8fb7cf72ba663898c Mon Sep 17 00:00:00 2001 From: messju Date: Wed, 6 Aug 2003 11:35:59 +0000 Subject: [PATCH] added optional parameter $cache_attrs to register_function() and register_block(). $cache_attrs is an array containing attribute- names that should be cached on calls to functions that have $cacheable set to false. --- NEWS | 6 +- libs/Smarty.class.php | 18 ++++-- libs/Smarty_Compiler.class.php | 66 ++++++++++++++------- libs/core/core.process_compiled_include.php | 4 ++ 4 files changed, 67 insertions(+), 27 deletions(-) diff --git a/NEWS b/NEWS index af4b9d46..73ba8a62 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ + - added optional parameter $cache_attrs to register_function() and + register_block(). $cache_attrs is an array containing attribute- + names that should be cached on calls to functions that have + $cacheable set to false. (messju) - enabled registration of class-methods as callbacks for the register_*- functions (use: array('classname', 'method_name')) as callback) (messju) - added filepath caching (Monte) @@ -46,7 +50,7 @@ - all in-code doc comments converted to phpDocumentor format (Greg) - moved strip from smarty core to plugin (Monte) - moved config_load from smarty core to plugin (Monte) - - added &$repeat-paramter to block-functions (messju) + - added &$repeat-parameter to block-functions (messju) - enabled hex-constants in function.math.php (messju) - enabled hex-constants (0x...) as function-attributes, inside if-statements and as modifier-parameters (messju) diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 332667d2..6fe9b6c5 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -1,4 +1,5 @@ _plugins['function'][$function] = - array($function_impl, null, null, false, $cacheable); + array($function_impl, null, null, false, $cacheable, $cache_attrs); + } /** @@ -787,10 +797,10 @@ class Smarty * @param string $block name of template block * @param string $block_impl PHP function to register */ - function register_block($block, $block_impl, $cacheable=true) + function register_block($block, $block_impl, $cacheable=true, $cache_attrs=null) { $this->_plugins['block'][$block] = - array($block_impl, null, null, false, $cacheable); + array($block_impl, null, null, false, $cacheable, $cache_attrs); } /** diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 880d160e..bcbd5c62 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -82,6 +82,7 @@ class Smarty_Compiler extends Smarty { var $_obj_call_regexp = null; var $_cacheable_state = 0; + var $_cache_attrs_count = 0; var $_nocache_count = 0; var $_cache_serial = null; var $_cache_include = null; @@ -655,16 +656,10 @@ class Smarty_Compiler extends Smarty { $this->_add_plugin('block', $tag_command); if ($start_tag) { - $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 = '_push_cacheable_state('block', $tag_command); - $output .= "\$_params = \$this->_tag_stack[] = array('$tag_command', array(".implode(',', (array)$arg_list).')); '; + $attrs = $this->_parse_attrs($tag_args); + $arg_list = $this->_compile_arg_list('block', $tag_command, $attrs, $_cache_attrs=''); + $output .= "$_cache_attrs\$_params = \$this->_tag_stack[] = array('$tag_command', array(".implode(',', $arg_list).')); '; $output .= $this->_compile_plugin_call('block', $tag_command).'($_params[1], null, $this, $_block_repeat=true); unset($_params);'; $output .= 'while ($_block_repeat) { ob_start(); ?>'; } else { @@ -693,26 +688,20 @@ class Smarty_Compiler extends Smarty { { $this->_add_plugin('function', $tag_command); - $arg_list = array(); + $_cacheable_state = $this->_push_cacheable_state('function', $tag_command); $attrs = $this->_parse_attrs($tag_args); + $arg_list = $this->_compile_arg_list('function', $tag_command, $attrs, $_cache_attrs=''); - foreach ($attrs as $arg_name => $arg_value) { - if (is_bool($arg_value)) - $arg_value = $arg_value ? 'true' : 'false'; - if (is_null($arg_value)) - $arg_value = 'null'; - $arg_list[] = "'$arg_name' => $arg_value"; - } - $_return = $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', (array)$arg_list)."), \$this)"; + $_return = $_cache_attrs . $this->_compile_plugin_call('function', $tag_command).'(array('.implode(',', $arg_list)."), \$this)"; if($tag_modifier != '') { $this->_parse_modifiers($_return, $tag_modifier); } - + if($_return != '') { - $_return = '_push_cacheable_state('function', $tag_command) - . 'echo ' . $_return . ';' . $this->_pop_cacheable_state('function', $tag_command) . "?>\n"; + $_return = '_pop_cacheable_state('function', $tag_command) . "?>\n"; } - + return $_return; } @@ -1293,6 +1282,39 @@ class Smarty_Compiler extends Smarty { } + function _compile_arg_list($type, $name, $attrs, &$cache_code) { + $arg_list = array(); + + if (isset($type) && isset($name) + && isset($this->_plugins[$type]) + && isset($this->_plugins[$type][$name]) + && empty($this->_plugins[$type][$name][4]) + && is_array($this->_plugins[$type][$name][5]) + ) { + /* we have a list of parameters that should be cached */ + $_cache_attrs = $this->_plugins[$type][$name][5]; + $_count = $this->_cache_attrs_count++; + $cache_code = "\$_cache_attrs =& \$this->_cache_info['cache_attrs']['$this->_cache_serial'][$_count];"; + + } else { + /* no parameters are cached */ + $_cache_attrs = null; + } + + foreach ($attrs as $arg_name => $arg_value) { + if (is_bool($arg_value)) + $arg_value = $arg_value ? 'true' : 'false'; + if (is_null($arg_value)) + $arg_value = 'null'; + if ($_cache_attrs && in_array($arg_name, $_cache_attrs)) { + $arg_list[] = "'$arg_name' => (\$this->_cache_including) ? \$_cache_attrs['$arg_name'] : (\$_cache_attrs['$arg_name']=$arg_value)"; + } else { + $arg_list[] = "'$arg_name' => $arg_value"; + } + } + return $arg_list; + } + /** * Parse is expression * diff --git a/libs/core/core.process_compiled_include.php b/libs/core/core.process_compiled_include.php index 486636f4..b1b66d77 100644 --- a/libs/core/core.process_compiled_include.php +++ b/libs/core/core.process_compiled_include.php @@ -16,12 +16,16 @@ function smarty_core_process_compiled_include($params, &$smarty) { + $_cache_including = $smarty->_cache_including; + $smarty->_cache_including = true; + $_return = $params['results']; foreach ($smarty->_cache_serials as $_include_file_path=>$_cache_serial) { $_return = preg_replace_callback('!(\{nocache\:('.$_cache_serial.')#(\d+)\})!s', array(&$smarty, '_process_compiled_include_callback'), $_return); } + $smarty->_cache_including = $_cache_including; return $_return; }