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.
This commit is contained in:
andrei
2002-04-16 20:04:06 +00:00
parent 22fe87c395
commit ad0ef5a6e0
6 changed files with 150 additions and 93 deletions

3
NEWS
View File

@@ -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 - added caching logic for expire times per cache file
(Norbert Rocher, Monte) (Norbert Rocher, Monte)
- fixed html_select_date when field separator is "/" - fixed html_select_date when field separator is "/"

View File

@@ -83,6 +83,8 @@ class Smarty
// to all templates // to all templates
var $undefined = null; // undefined variables in $global_assign will be var $undefined = null; // undefined variables in $global_assign will be
// created with this value // 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: var $compile_check = true; // whether to check for compiling step or not:
// This is generally set to false once the // This is generally set to false once the
// application is entered into production and // application is entered into production and
@@ -170,14 +172,15 @@ class Smarty
var $_smarty_debug_info = array(); // debugging information for debug console var $_smarty_debug_info = array(); // debugging information for debug console
var $_cache_info = array(); // info that makes up a cache file var $_cache_info = array(); // info that makes up a cache file
var $_plugins = array( // table keeping track of plugins var $_plugins = array( // table keeping track of plugins
'modifier' => array(), 'modifier' => array(),
'function' => array(), 'function' => array(),
'block' => array(), 'block' => array(),
'compiler' => array(), 'compiler' => array(),
'prefilter' => array(), 'prefilter' => array(),
'postfilter'=> array(), 'postfilter' => array(),
'resource' => array(), 'outputfilter' => array(),
'insert' => array()); 'resource' => array(),
'insert' => array());
/*======================================================================*\ /*======================================================================*\
@@ -406,6 +409,25 @@ class Smarty
$this->_plugins['postfilter'][$function] = false; $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() Function: clear_cache()
Purpose: clear cached content for the given template and cache id Purpose: clear cached content for the given template and cache id
@@ -588,23 +610,30 @@ class Smarty
$this->_config = array(array('vars' => array(), $this->_config = array(array('vars' => array(),
'files' => 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 // if we just need to display the results, don't perform output
// buffering - for speed // buffering - for speed
if ($_smarty_display && !$this->caching) { if ($_smarty_display && !$this->caching && count($this->_plugins['outputfilter']) == 0) {
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);
} }
} else { } else {
ob_start(); 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(); $_smarty_results = ob_get_contents();
ob_end_clean(); ob_end_clean();
foreach ($this->_plugins['outputfilter'] as $output_filter) {
$_smarty_results = $output_filter[0]($_smarty_results, $this);
}
} }
if ($this->caching) { if ($this->caching) {
@@ -1017,10 +1046,10 @@ function _generate_debug_output() {
extract($this->_tpl_vars); extract($this->_tpl_vars);
array_unshift($this->_config, $this->_config[0]); 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)) { if ($this->_process_template($_smarty_include_tpl_file, $_smarty_compile_path)) {
include($compile_path); include($_smarty_compile_path);
} }
array_shift($this->_config); 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);
}
}
} }
/*======================================================================*\ /*======================================================================*\

View File

@@ -49,7 +49,6 @@ class Smarty_Compiler extends Smarty {
var $_current_line_no = 1; // line number for error messages var $_current_line_no = 1; // line number for error messages
var $_capture_stack = array(); // keeps track of nested capture buffers var $_capture_stack = array(); // keeps track of nested capture buffers
var $_plugin_info = array(); // keeps track of plugins to load var $_plugin_info = array(); // keeps track of plugins to load
var $_filters_loaded = false;
var $_init_smarty_vars = false; var $_init_smarty_vars = false;
@@ -67,10 +66,7 @@ class Smarty_Compiler extends Smarty {
} }
} }
if (!$this->_filters_loaded) { $this->_load_filters();
$this->_load_filters();
$this->_filters_loaded = true;
}
$this->_current_file = $tpl_file; $this->_current_file = $tpl_file;
$this->_current_line_no = 1; $this->_current_line_no = 1;
@@ -1355,36 +1351,29 @@ class Smarty_Compiler extends Smarty {
return $compiled_ref; return $compiled_ref;
} }
/*======================================================================*\ /*======================================================================*\
Function: _load_filters Function: _load_filters
Purpose: load pre- and post-filters Purpose: load pre- and post-filters
\*======================================================================*/ \*======================================================================*/
function _load_filters() function _load_filters()
{ {
$plugins_dir = SMARTY_DIR . $this->plugins_dir; if (count($this->_plugins['prefilter']) > 0) {
$handle = opendir($plugins_dir); foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
while ($entry = readdir($handle)) { if ($prefilter === false) {
$parts = explode('.', $entry, 3); unset($this->_plugins['prefilter'][$filter_name]);
if ($entry == '.' || $this->_load_plugins(array(array('prefilter', $filter_name, null, null, false)));
$entry == '..' || }
count($parts) < 3 || }
$parts[2] != 'php' || }
($parts[0] != 'prefilter' && if (count($this->_plugins['postfilter']) > 0) {
$parts[0] != 'postfilter') || foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
(isset($this->_plugins[$parts[0]][$parts[1]]) && if ($postfilter === false) {
$this->_plugins[$parts[0]][$parts[1]] === false)) unset($this->_plugins['postfilter'][$filter_name]);
continue; $this->_load_plugins(array(array('postfilter', $filter_name, null, null, false)));
}
$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);
} }
} }
closedir($handle);
} }

2
TODO
View File

@@ -3,8 +3,8 @@
<?php echo "<?php exit(); ?>"; ?> <?php echo "<?php exit(); ?>"; ?>
* support implementations of prefiltes, mods, and others as class methods. * support implementations of prefiltes, mods, and others as class methods.
* possibly implement default modifiers that apply to variables upon display * 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 * ability to concatenate values/strings together
* fix all E_NOTICE warnings * fix all E_NOTICE warnings
* make simple math easier * make simple math easier
* caching all but parts of the template * caching all but parts of the template
* change plugins so $smarty variable always comes first

View File

@@ -83,6 +83,8 @@ class Smarty
// to all templates // to all templates
var $undefined = null; // undefined variables in $global_assign will be var $undefined = null; // undefined variables in $global_assign will be
// created with this value // 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: var $compile_check = true; // whether to check for compiling step or not:
// This is generally set to false once the // This is generally set to false once the
// application is entered into production and // application is entered into production and
@@ -170,14 +172,15 @@ class Smarty
var $_smarty_debug_info = array(); // debugging information for debug console var $_smarty_debug_info = array(); // debugging information for debug console
var $_cache_info = array(); // info that makes up a cache file var $_cache_info = array(); // info that makes up a cache file
var $_plugins = array( // table keeping track of plugins var $_plugins = array( // table keeping track of plugins
'modifier' => array(), 'modifier' => array(),
'function' => array(), 'function' => array(),
'block' => array(), 'block' => array(),
'compiler' => array(), 'compiler' => array(),
'prefilter' => array(), 'prefilter' => array(),
'postfilter'=> array(), 'postfilter' => array(),
'resource' => array(), 'outputfilter' => array(),
'insert' => array()); 'resource' => array(),
'insert' => array());
/*======================================================================*\ /*======================================================================*\
@@ -406,6 +409,25 @@ class Smarty
$this->_plugins['postfilter'][$function] = false; $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() Function: clear_cache()
Purpose: clear cached content for the given template and cache id Purpose: clear cached content for the given template and cache id
@@ -588,23 +610,30 @@ class Smarty
$this->_config = array(array('vars' => array(), $this->_config = array(array('vars' => array(),
'files' => 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 // if we just need to display the results, don't perform output
// buffering - for speed // buffering - for speed
if ($_smarty_display && !$this->caching) { if ($_smarty_display && !$this->caching && count($this->_plugins['outputfilter']) == 0) {
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);
} }
} else { } else {
ob_start(); 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(); $_smarty_results = ob_get_contents();
ob_end_clean(); ob_end_clean();
foreach ($this->_plugins['outputfilter'] as $output_filter) {
$_smarty_results = $output_filter[0]($_smarty_results, $this);
}
} }
if ($this->caching) { if ($this->caching) {
@@ -1017,10 +1046,10 @@ function _generate_debug_output() {
extract($this->_tpl_vars); extract($this->_tpl_vars);
array_unshift($this->_config, $this->_config[0]); 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)) { if ($this->_process_template($_smarty_include_tpl_file, $_smarty_compile_path)) {
include($compile_path); include($_smarty_compile_path);
} }
array_shift($this->_config); 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);
}
}
} }
/*======================================================================*\ /*======================================================================*\

View File

@@ -49,7 +49,6 @@ class Smarty_Compiler extends Smarty {
var $_current_line_no = 1; // line number for error messages var $_current_line_no = 1; // line number for error messages
var $_capture_stack = array(); // keeps track of nested capture buffers var $_capture_stack = array(); // keeps track of nested capture buffers
var $_plugin_info = array(); // keeps track of plugins to load var $_plugin_info = array(); // keeps track of plugins to load
var $_filters_loaded = false;
var $_init_smarty_vars = false; var $_init_smarty_vars = false;
@@ -67,10 +66,7 @@ class Smarty_Compiler extends Smarty {
} }
} }
if (!$this->_filters_loaded) { $this->_load_filters();
$this->_load_filters();
$this->_filters_loaded = true;
}
$this->_current_file = $tpl_file; $this->_current_file = $tpl_file;
$this->_current_line_no = 1; $this->_current_line_no = 1;
@@ -1355,36 +1351,29 @@ class Smarty_Compiler extends Smarty {
return $compiled_ref; return $compiled_ref;
} }
/*======================================================================*\ /*======================================================================*\
Function: _load_filters Function: _load_filters
Purpose: load pre- and post-filters Purpose: load pre- and post-filters
\*======================================================================*/ \*======================================================================*/
function _load_filters() function _load_filters()
{ {
$plugins_dir = SMARTY_DIR . $this->plugins_dir; if (count($this->_plugins['prefilter']) > 0) {
$handle = opendir($plugins_dir); foreach ($this->_plugins['prefilter'] as $filter_name => $prefilter) {
while ($entry = readdir($handle)) { if ($prefilter === false) {
$parts = explode('.', $entry, 3); unset($this->_plugins['prefilter'][$filter_name]);
if ($entry == '.' || $this->_load_plugins(array(array('prefilter', $filter_name, null, null, false)));
$entry == '..' || }
count($parts) < 3 || }
$parts[2] != 'php' || }
($parts[0] != 'prefilter' && if (count($this->_plugins['postfilter']) > 0) {
$parts[0] != 'postfilter') || foreach ($this->_plugins['postfilter'] as $filter_name => $postfilter) {
(isset($this->_plugins[$parts[0]][$parts[1]]) && if ($postfilter === false) {
$this->_plugins[$parts[0]][$parts[1]] === false)) unset($this->_plugins['postfilter'][$filter_name]);
continue; $this->_load_plugins(array(array('postfilter', $filter_name, null, null, false)));
}
$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);
} }
} }
closedir($handle);
} }