From ad0ef5a6e01a965373a74b0d497b6f6614b2f9cd Mon Sep 17 00:00:00 2001 From: andrei Date: Tue, 16 Apr 2002 20:04:06 +0000 Subject: [PATCH] Changed the way filters are loaded, which now has to be done explicitly, either through load_filter() API or by filling in $autoload_filters variable. Also renamed internal variable to avoid namespace pollution. --- NEWS | 3 ++ Smarty.class.php | 74 +++++++++++++++++++++++++--------- Smarty_Compiler.class.php | 45 ++++++++------------- TODO | 2 +- libs/Smarty.class.php | 74 +++++++++++++++++++++++++--------- libs/Smarty_Compiler.class.php | 45 ++++++++------------- 6 files changed, 150 insertions(+), 93 deletions(-) diff --git a/NEWS b/NEWS index f9249024..72753393 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,6 @@ + - introduced output filters. (Andrei) + - changed the way filters are loaded, added load_filter() + API function and $autoload_filters variable. (Andrei) - added caching logic for expire times per cache file (Norbert Rocher, Monte) - fixed html_select_date when field separator is "/" diff --git a/Smarty.class.php b/Smarty.class.php index 054ded06..a68c450d 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -83,6 +83,8 @@ class Smarty // to all templates var $undefined = null; // undefined variables in $global_assign will be // created with this value + var $autoload_filters = array(); // indicates which filters will be auto-loaded + var $compile_check = true; // whether to check for compiling step or not: // This is generally set to false once the // application is entered into production and @@ -170,14 +172,15 @@ class Smarty var $_smarty_debug_info = array(); // debugging information for debug console var $_cache_info = array(); // info that makes up a cache file var $_plugins = array( // table keeping track of plugins - 'modifier' => array(), - 'function' => array(), - 'block' => array(), - 'compiler' => array(), - 'prefilter' => array(), - 'postfilter'=> array(), - 'resource' => array(), - 'insert' => array()); + 'modifier' => array(), + 'function' => array(), + 'block' => array(), + 'compiler' => array(), + 'prefilter' => array(), + 'postfilter' => array(), + 'outputfilter' => array(), + 'resource' => array(), + 'insert' => array()); /*======================================================================*\ @@ -406,6 +409,25 @@ class Smarty $this->_plugins['postfilter'][$function] = false; } +/*======================================================================*\ + Function: load_filter() + Purpose: load a filter of specified type and name +\*======================================================================*/ + function load_filter($type, $name) + { + switch ($type) { + case 'output': + $this->_load_plugins(array(array($type . 'filter', $name, null, null, false))); + break; + + case 'pre': + case 'post': + if (!isset($this->_plugins[$type . 'filter'][$name])) + $this->_plugins[$type . 'filter'][$name] = false; + break; + } + } + /*======================================================================*\ Function: clear_cache() Purpose: clear cached content for the given template and cache id @@ -588,23 +610,30 @@ class Smarty $this->_config = array(array('vars' => array(), 'files' => array())); - $compile_path = $this->_get_compile_path($_smarty_tpl_file); + if (count($this->autoload_filters)) + $this->_autoload_filters(); + + $_smarty_compile_path = $this->_get_compile_path($_smarty_tpl_file); // if we just need to display the results, don't perform output // buffering - for speed - if ($_smarty_display && !$this->caching) { - if ($this->_process_template($_smarty_tpl_file, $compile_path)) + if ($_smarty_display && !$this->caching && count($this->_plugins['outputfilter']) == 0) { + if ($this->_process_template($_smarty_tpl_file, $_smarty_compile_path)) { - include($compile_path); + include($_smarty_compile_path); } } else { ob_start(); - if ($this->_process_template($_smarty_tpl_file, $compile_path)) + if ($this->_process_template($_smarty_tpl_file, $_smarty_compile_path)) { - include($compile_path); + include($_smarty_compile_path); } $_smarty_results = ob_get_contents(); ob_end_clean(); + + foreach ($this->_plugins['outputfilter'] as $output_filter) { + $_smarty_results = $output_filter[0]($_smarty_results, $this); + } } if ($this->caching) { @@ -1017,10 +1046,10 @@ function _generate_debug_output() { extract($this->_tpl_vars); array_unshift($this->_config, $this->_config[0]); - $compile_path = $this->_get_compile_path($_smarty_include_tpl_file); + $_smarty_compile_path = $this->_get_compile_path($_smarty_include_tpl_file); - if ($this->_process_template($_smarty_include_tpl_file, $compile_path)) { - include($compile_path); + if ($this->_process_template($_smarty_include_tpl_file, $_smarty_compile_path)) { + include($_smarty_compile_path); } array_shift($this->_config); @@ -1722,8 +1751,17 @@ function _run_insert_handler($args) } } - function _init_conf_obj() +/*======================================================================*\ + Function: _autoload_filters() + Purpose: automatically load a set of filters +\*======================================================================*/ + function _autoload_filters() { + foreach ($this->autoload_filters as $filter_type => $filters) { + foreach ($filters as $filter) { + $this->load_filter($filter_type, $filter); + } + } } /*======================================================================*\ diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index e23453a7..c22a1612 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -49,7 +49,6 @@ class Smarty_Compiler extends Smarty { var $_current_line_no = 1; // line number for error messages var $_capture_stack = array(); // keeps track of nested capture buffers var $_plugin_info = array(); // keeps track of plugins to load - var $_filters_loaded = false; var $_init_smarty_vars = false; @@ -67,10 +66,7 @@ class Smarty_Compiler extends Smarty { } } - if (!$this->_filters_loaded) { - $this->_load_filters(); - $this->_filters_loaded = true; - } + $this->_load_filters(); $this->_current_file = $tpl_file; $this->_current_line_no = 1; @@ -1355,38 +1351,31 @@ class Smarty_Compiler extends Smarty { return $compiled_ref; } + /*======================================================================*\ Function: _load_filters Purpose: load pre- and post-filters \*======================================================================*/ function _load_filters() { - $plugins_dir = SMARTY_DIR . $this->plugins_dir; - $handle = opendir($plugins_dir); - while ($entry = readdir($handle)) { - $parts = explode('.', $entry, 3); - if ($entry == '.' || - $entry == '..' || - count($parts) < 3 || - $parts[2] != 'php' || - ($parts[0] != 'prefilter' && - $parts[0] != 'postfilter') || - (isset($this->_plugins[$parts[0]][$parts[1]]) && - $this->_plugins[$parts[0]][$parts[1]] === false)) - continue; - - $plugin_file = $plugins_dir . DIR_SEP . $entry; - include_once $plugin_file; - $plugin_func = 'smarty_' . $parts[0] . '_' . $parts[1]; - if (!function_exists($plugin_func)) { - $this->_trigger_plugin_error("Smarty plugin error: plugin function $plugin_func() not found in $plugin_file"); - } else { - $this->_plugins[$parts[0]][$parts[1]] = array($plugin_func, null, null, true); + if (count($this->_plugins['prefilter']) > 0) { + foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) { + if ($prefilter === false) { + unset($this->_plugins['prefilter'][$filter_name]); + $this->_load_plugins(array(array('prefilter', $filter_name, null, null, false))); + } + } + } + if (count($this->_plugins['postfilter']) > 0) { + foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) { + if ($postfilter === false) { + unset($this->_plugins['postfilter'][$filter_name]); + $this->_load_plugins(array(array('postfilter', $filter_name, null, null, false))); + } } } - closedir($handle); } - + /*======================================================================*\ Function: _syntax_error diff --git a/TODO b/TODO index e6893166..6b0d3395 100644 --- a/TODO +++ b/TODO @@ -3,8 +3,8 @@ "; ?> * 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]} * ability to concatenate values/strings together * fix all E_NOTICE warnings * make simple math easier * caching all but parts of the template +* change plugins so $smarty variable always comes first diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 054ded06..a68c450d 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -83,6 +83,8 @@ class Smarty // to all templates var $undefined = null; // undefined variables in $global_assign will be // created with this value + var $autoload_filters = array(); // indicates which filters will be auto-loaded + var $compile_check = true; // whether to check for compiling step or not: // This is generally set to false once the // application is entered into production and @@ -170,14 +172,15 @@ class Smarty var $_smarty_debug_info = array(); // debugging information for debug console var $_cache_info = array(); // info that makes up a cache file var $_plugins = array( // table keeping track of plugins - 'modifier' => array(), - 'function' => array(), - 'block' => array(), - 'compiler' => array(), - 'prefilter' => array(), - 'postfilter'=> array(), - 'resource' => array(), - 'insert' => array()); + 'modifier' => array(), + 'function' => array(), + 'block' => array(), + 'compiler' => array(), + 'prefilter' => array(), + 'postfilter' => array(), + 'outputfilter' => array(), + 'resource' => array(), + 'insert' => array()); /*======================================================================*\ @@ -406,6 +409,25 @@ class Smarty $this->_plugins['postfilter'][$function] = false; } +/*======================================================================*\ + Function: load_filter() + Purpose: load a filter of specified type and name +\*======================================================================*/ + function load_filter($type, $name) + { + switch ($type) { + case 'output': + $this->_load_plugins(array(array($type . 'filter', $name, null, null, false))); + break; + + case 'pre': + case 'post': + if (!isset($this->_plugins[$type . 'filter'][$name])) + $this->_plugins[$type . 'filter'][$name] = false; + break; + } + } + /*======================================================================*\ Function: clear_cache() Purpose: clear cached content for the given template and cache id @@ -588,23 +610,30 @@ class Smarty $this->_config = array(array('vars' => array(), 'files' => array())); - $compile_path = $this->_get_compile_path($_smarty_tpl_file); + if (count($this->autoload_filters)) + $this->_autoload_filters(); + + $_smarty_compile_path = $this->_get_compile_path($_smarty_tpl_file); // if we just need to display the results, don't perform output // buffering - for speed - if ($_smarty_display && !$this->caching) { - if ($this->_process_template($_smarty_tpl_file, $compile_path)) + if ($_smarty_display && !$this->caching && count($this->_plugins['outputfilter']) == 0) { + if ($this->_process_template($_smarty_tpl_file, $_smarty_compile_path)) { - include($compile_path); + include($_smarty_compile_path); } } else { ob_start(); - if ($this->_process_template($_smarty_tpl_file, $compile_path)) + if ($this->_process_template($_smarty_tpl_file, $_smarty_compile_path)) { - include($compile_path); + include($_smarty_compile_path); } $_smarty_results = ob_get_contents(); ob_end_clean(); + + foreach ($this->_plugins['outputfilter'] as $output_filter) { + $_smarty_results = $output_filter[0]($_smarty_results, $this); + } } if ($this->caching) { @@ -1017,10 +1046,10 @@ function _generate_debug_output() { extract($this->_tpl_vars); array_unshift($this->_config, $this->_config[0]); - $compile_path = $this->_get_compile_path($_smarty_include_tpl_file); + $_smarty_compile_path = $this->_get_compile_path($_smarty_include_tpl_file); - if ($this->_process_template($_smarty_include_tpl_file, $compile_path)) { - include($compile_path); + if ($this->_process_template($_smarty_include_tpl_file, $_smarty_compile_path)) { + include($_smarty_compile_path); } array_shift($this->_config); @@ -1722,8 +1751,17 @@ function _run_insert_handler($args) } } - function _init_conf_obj() +/*======================================================================*\ + Function: _autoload_filters() + Purpose: automatically load a set of filters +\*======================================================================*/ + function _autoload_filters() { + foreach ($this->autoload_filters as $filter_type => $filters) { + foreach ($filters as $filter) { + $this->load_filter($filter_type, $filter); + } + } } /*======================================================================*\ diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index e23453a7..c22a1612 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -49,7 +49,6 @@ class Smarty_Compiler extends Smarty { var $_current_line_no = 1; // line number for error messages var $_capture_stack = array(); // keeps track of nested capture buffers var $_plugin_info = array(); // keeps track of plugins to load - var $_filters_loaded = false; var $_init_smarty_vars = false; @@ -67,10 +66,7 @@ class Smarty_Compiler extends Smarty { } } - if (!$this->_filters_loaded) { - $this->_load_filters(); - $this->_filters_loaded = true; - } + $this->_load_filters(); $this->_current_file = $tpl_file; $this->_current_line_no = 1; @@ -1355,38 +1351,31 @@ class Smarty_Compiler extends Smarty { return $compiled_ref; } + /*======================================================================*\ Function: _load_filters Purpose: load pre- and post-filters \*======================================================================*/ function _load_filters() { - $plugins_dir = SMARTY_DIR . $this->plugins_dir; - $handle = opendir($plugins_dir); - while ($entry = readdir($handle)) { - $parts = explode('.', $entry, 3); - if ($entry == '.' || - $entry == '..' || - count($parts) < 3 || - $parts[2] != 'php' || - ($parts[0] != 'prefilter' && - $parts[0] != 'postfilter') || - (isset($this->_plugins[$parts[0]][$parts[1]]) && - $this->_plugins[$parts[0]][$parts[1]] === false)) - continue; - - $plugin_file = $plugins_dir . DIR_SEP . $entry; - include_once $plugin_file; - $plugin_func = 'smarty_' . $parts[0] . '_' . $parts[1]; - if (!function_exists($plugin_func)) { - $this->_trigger_plugin_error("Smarty plugin error: plugin function $plugin_func() not found in $plugin_file"); - } else { - $this->_plugins[$parts[0]][$parts[1]] = array($plugin_func, null, null, true); + if (count($this->_plugins['prefilter']) > 0) { + foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) { + if ($prefilter === false) { + unset($this->_plugins['prefilter'][$filter_name]); + $this->_load_plugins(array(array('prefilter', $filter_name, null, null, false))); + } + } + } + if (count($this->_plugins['postfilter']) > 0) { + foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) { + if ($postfilter === false) { + unset($this->_plugins['postfilter'][$filter_name]); + $this->_load_plugins(array(array('postfilter', $filter_name, null, null, false))); + } } } - closedir($handle); } - + /*======================================================================*\ Function: _syntax_error