From fa1dc015ee5056c0c194db86daa2e87d0a32dfb4 Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 28 Sep 2001 21:39:57 +0000 Subject: [PATCH] Added postfilter functions. --- NEWS | 2 ++ Smarty.class.php | 34 +++++++++++++++++++++++++++++++--- Smarty_Compiler.class.php | 14 +++++++++++++- libs/Smarty.class.php | 34 +++++++++++++++++++++++++++++++--- libs/Smarty_Compiler.class.php | 14 +++++++++++++- 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index da5bea62..135832c6 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,5 @@ + - added support for postfilter functions that are applied to compiled + template right after compilation. (Andrei) - fixed the name of clear_compile_tpl() API function to clear_compiled_tpl. (Andrei) - added fix for removing comments so that the line numbers are reported diff --git a/Smarty.class.php b/Smarty.class.php index 3d7a5b48..3767e6b5 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -170,9 +170,11 @@ class Smarty var $compiler_class = 'Smarty_Compiler'; // the compiler class used by // Smarty to compile templates - var $resource_funcs = array(); // what functions resource handlers are mapped to - var $prefilter_funcs = array(); // what functions templates are prefiltered through + var $resource_funcs = array(); // functions that resource handlers are mapped to + var $prefilter_funcs = array(); // functions that templates are filtered through // before being compiled + var $postfilter_funcs = array(); // functions that compiled templates are filtered + // through after compilation var $request_vars_order = "EGPCS"; // the order in which request variables are // registered, similar to variables_order @@ -366,7 +368,7 @@ class Smarty /*======================================================================*\ Function: unregister_prefilter - Purpose: Unregisters a prefilter + Purpose: Unregisters a prefilter function \*======================================================================*/ function unregister_prefilter($function_name) { @@ -379,6 +381,31 @@ class Smarty $this->prefilter_funcs = $tmp_array; } +/*======================================================================*\ + Function: register_postfilter + Purpose: Registers a postfilter function to apply + to a compiled template after compilation +\*======================================================================*/ + function register_postfilter($function_name) + { + $this->postfilter_funcs[] = $function_name; + } + +/*======================================================================*\ + Function: unregister_postfilter + Purpose: Unregisters a postfilter function +\*======================================================================*/ + function unregister_postfilter($function_name) + { + $tmp_array = array(); + foreach($this->postfilter_funcs as $curr_func) { + if ($curr_func != $function_name) { + $tmp_array[] = $curr_func; + } + } + $this->postfilter_funcs = $tmp_array; + } + /*======================================================================*\ Function: clear_cache() Purpose: clear cached content for the given template and cache id @@ -786,6 +813,7 @@ function _generate_debug_output() { $smarty_compiler->custom_mods = $this->custom_mods; $smarty_compiler->_version = $this->_version; $smarty_compiler->prefilter_funcs = $this->prefilter_funcs; + $smarty_compiler->postfilter_funcs = $this->postfilter_funcs; $smarty_compiler->compiler_funcs = $this->compiler_funcs; $smarty_compiler->security = $this->security; $smarty_compiler->secure_dir = $this->secure_dir; diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index 43b88ed8..46b8310f 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -63,7 +63,7 @@ class Smarty_Compiler extends Smarty { } } - // run template source through functions registered in prefilter_funcs + // run template source through prefilter functions if (is_array($this->prefilter_funcs) && count($this->prefilter_funcs) > 0) { foreach ($this->prefilter_funcs as $prefilter) { if (function_exists($prefilter)) { @@ -162,6 +162,18 @@ class Smarty_Compiler extends Smarty { $template_header = "_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n"; $template_header .= " compiled from ".$tpl_file." */ ?>\n"; $template_compiled = $template_header.$template_compiled; + + + // run compiled template through postfilter functions + if (is_array($this->postfilter_funcs) && count($this->postfilter_funcs) > 0) { + foreach ($this->postfilter_funcs as $postfilter) { + if (function_exists($postfilter)) { + $template_compiled = $postfilter($template_compiled, $this); + } else { + $this->_trigger_error_msg("postfilter function $postfilter does not exist."); + } + } + } return true; } diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 3d7a5b48..3767e6b5 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -170,9 +170,11 @@ class Smarty var $compiler_class = 'Smarty_Compiler'; // the compiler class used by // Smarty to compile templates - var $resource_funcs = array(); // what functions resource handlers are mapped to - var $prefilter_funcs = array(); // what functions templates are prefiltered through + var $resource_funcs = array(); // functions that resource handlers are mapped to + var $prefilter_funcs = array(); // functions that templates are filtered through // before being compiled + var $postfilter_funcs = array(); // functions that compiled templates are filtered + // through after compilation var $request_vars_order = "EGPCS"; // the order in which request variables are // registered, similar to variables_order @@ -366,7 +368,7 @@ class Smarty /*======================================================================*\ Function: unregister_prefilter - Purpose: Unregisters a prefilter + Purpose: Unregisters a prefilter function \*======================================================================*/ function unregister_prefilter($function_name) { @@ -379,6 +381,31 @@ class Smarty $this->prefilter_funcs = $tmp_array; } +/*======================================================================*\ + Function: register_postfilter + Purpose: Registers a postfilter function to apply + to a compiled template after compilation +\*======================================================================*/ + function register_postfilter($function_name) + { + $this->postfilter_funcs[] = $function_name; + } + +/*======================================================================*\ + Function: unregister_postfilter + Purpose: Unregisters a postfilter function +\*======================================================================*/ + function unregister_postfilter($function_name) + { + $tmp_array = array(); + foreach($this->postfilter_funcs as $curr_func) { + if ($curr_func != $function_name) { + $tmp_array[] = $curr_func; + } + } + $this->postfilter_funcs = $tmp_array; + } + /*======================================================================*\ Function: clear_cache() Purpose: clear cached content for the given template and cache id @@ -786,6 +813,7 @@ function _generate_debug_output() { $smarty_compiler->custom_mods = $this->custom_mods; $smarty_compiler->_version = $this->_version; $smarty_compiler->prefilter_funcs = $this->prefilter_funcs; + $smarty_compiler->postfilter_funcs = $this->postfilter_funcs; $smarty_compiler->compiler_funcs = $this->compiler_funcs; $smarty_compiler->security = $this->security; $smarty_compiler->secure_dir = $this->secure_dir; diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 43b88ed8..46b8310f 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -63,7 +63,7 @@ class Smarty_Compiler extends Smarty { } } - // run template source through functions registered in prefilter_funcs + // run template source through prefilter functions if (is_array($this->prefilter_funcs) && count($this->prefilter_funcs) > 0) { foreach ($this->prefilter_funcs as $prefilter) { if (function_exists($prefilter)) { @@ -162,6 +162,18 @@ class Smarty_Compiler extends Smarty { $template_header = "_version.", created on ".strftime("%Y-%m-%d %H:%M:%S")."\n"; $template_header .= " compiled from ".$tpl_file." */ ?>\n"; $template_compiled = $template_header.$template_compiled; + + + // run compiled template through postfilter functions + if (is_array($this->postfilter_funcs) && count($this->postfilter_funcs) > 0) { + foreach ($this->postfilter_funcs as $postfilter) { + if (function_exists($postfilter)) { + $template_compiled = $postfilter($template_compiled, $this); + } else { + $this->_trigger_error_msg("postfilter function $postfilter does not exist."); + } + } + } return true; }