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.
This commit is contained in:
messju
2003-08-06 11:35:59 +00:00
parent 4aa9036e73
commit f15be25a42
4 changed files with 67 additions and 27 deletions

6
NEWS
View File

@@ -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)

View File

@@ -1,4 +1,5 @@
<?php
/**
* Project: Smarty: the PHP compiling template engine
* File: Smarty.class.php
@@ -578,6 +579,14 @@ class Smarty
*/
var $_cache_include = null;
/**
* indicate if the current code is used in a compiled
* include
*
* @var string
*/
var $_cache_including = false;
/**
* cached file paths
*
@@ -737,10 +746,11 @@ class Smarty
* @param string $function the name of the template function
* @param string $function_impl the name of the PHP function to register
*/
function register_function($function, $function_impl, $cacheable=true)
function register_function($function, $function_impl, $cacheable=true, $cache_attrs=null)
{
$this->_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);
}
/**

View File

@@ -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 = '<?php ' . $this->_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 = '<?php ' . $this->_push_cacheable_state('function', $tag_command)
. 'echo ' . $_return . ';' . $this->_pop_cacheable_state('function', $tag_command) . "?>\n";
$_return = '<?php ' . $_cacheable_state . 'echo ' . $_return . ';'
. $this->_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
*

View File

@@ -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;
}