diff --git a/change_log.txt b/change_log.txt index adfa9807..cd5d5f93 100644 --- a/change_log.txt +++ b/change_log.txt @@ -1,3 +1,6 @@ +04/10/2009 +- implementation of a 'variable' filter as replacement for default modifier + 04/09/2009 - fixed execution of filters defined by classes - compile the always the content of {block} tags to make shure that the filters are running over it diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 9b33647b..90f948f7 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -37,14 +37,14 @@ */ if (!defined('SMARTY_DIR')) { define('SMARTY_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR); -} +} /** -* define variable scopes +* define variable scopes */ -define('SMARTY_LOCAL_SCOPE',0); -define('SMARTY_PARENT_SCOPE',1); -define('SMARTY_ROOT_SCOPE',2); +define('SMARTY_LOCAL_SCOPE', 0); +define('SMARTY_PARENT_SCOPE', 1); +define('SMARTY_ROOT_SCOPE', 2); /** * load required base class for creation of the smarty object @@ -62,7 +62,7 @@ class Smarty extends Smarty_Internal_TemplateBase { // display error on not assigned variabled static $error_unassigned = false; // template directory - public $template_dir = null; + public $template_dir = null; // default template handler public $default_template_handler_func = null; // compile directory @@ -110,7 +110,7 @@ class Smarty extends Smarty_Internal_TemplateBase { // config var settings public $config_overwrite = true; //Controls whether variables with the same name overwrite each other. public $config_booleanize = true; //Controls whether config values of on/true/yes and off/false/no get converted to boolean - public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file. + public $config_read_hidden = true; //Controls whether hidden config sections/vars are read from the file. // config vars public $config_vars = array(); // assigned tpl vars @@ -137,8 +137,6 @@ class Smarty extends Smarty_Internal_TemplateBase { public $exception_handler = array('SmartyException', 'getStaticException'); // cached template objects static $template_objects = null; - // autoload filter - public $autoload_filters = array(); // check If-Modified-Since headers public $cache_modified_check = false; // registered plugins @@ -155,6 +153,10 @@ class Smarty extends Smarty_Internal_TemplateBase { public $registered_filters = array(); // filter handler public $filter_handler = null; + // autoload filter + public $autoload_filters = array(); + // status of filter on variable output + public $variable_filter = false; // cache resorce objects public $cache_resource_objects = array(); // write file object @@ -162,7 +164,7 @@ class Smarty extends Smarty_Internal_TemplateBase { // global smarty vars public $_smarty_vars = array(); // start time for execution time calculation - public $start_time = 0; + public $start_time = 0; // set default time zone public $set_timezone = true; /** @@ -194,6 +196,9 @@ class Smarty extends Smarty_Internal_TemplateBase { $this->loadPlugin($this->template_class); $this->loadPlugin('Smarty_Internal_Plugin_Handler'); $this->plugin_handler = new Smarty_Internal_Plugin_Handler; + $this->loadPlugin('Smarty_Internal_Run_Filter'); + $this->filter_handler = new Smarty_Internal_Run_Filter; + if (!$this->debugging && $this->debugging_ctrl == 'URL') { $_query_string = $this->request_use_auto_globals ? isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING']:'' : isset($GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING']) ? $GLOBALS['HTTP_SERVER_VARS']['QUERY_STRING']:''; if (false !== strpos($_query_string, $this->smarty_debug_id)) { diff --git a/libs/plugins/variablefilter.htmlspecialchars.php b/libs/plugins/variablefilter.htmlspecialchars.php new file mode 100644 index 00000000..1130a992 --- /dev/null +++ b/libs/plugins/variablefilter.htmlspecialchars.php @@ -0,0 +1,21 @@ + diff --git a/libs/sysplugins/internal.compile_print_expression.php b/libs/sysplugins/internal.compile_print_expression.php index 040b5e9a..478ab2ce 100644 --- a/libs/sysplugins/internal.compile_print_expression.php +++ b/libs/sysplugins/internal.compile_print_expression.php @@ -21,9 +21,9 @@ class Smarty_Internal_Compile_Print_Expression extends Smarty_Internal_CompileBa */ public function compile($args, $compiler) { - $this->compiler = $compiler; + $this->compiler = $compiler; $this->required_attributes = array('value'); - $this->optional_attributes = array('assign','nocache'); + $this->optional_attributes = array('assign', 'nocache', 'filter'); // check and get attributes $_attr = $this->_get_attributes($args); @@ -33,13 +33,17 @@ class Smarty_Internal_Compile_Print_Expression extends Smarty_Internal_CompileBa } } + if (!isset($_attr['filter'])) { + $_attr['filter'] = 'null'; + } + if (isset($_attr['assign'])) { // assign output to variable $output = 'assign(' . $_attr['assign'] . ',' . $_attr['value'] . ');?>'; } else { // display value $this->compiler->has_output = true; - $output = ''; + $output = 'smarty->filter_handler->execute(\'variable\', ' . $_attr['value'] . ',' . $_attr['filter'] . ');?>'; } return $output; } diff --git a/libs/sysplugins/internal.compiler.php b/libs/sysplugins/internal.compiler.php index e354b1c1..ceea54eb 100644 --- a/libs/sysplugins/internal.compiler.php +++ b/libs/sysplugins/internal.compiler.php @@ -32,10 +32,6 @@ class Smarty_Internal_Compiler extends Smarty_Internal_Base { $this->smarty->loadPlugin($parser_class); $this->lexer_class = $lexer_class; $this->parser_class = $parser_class; - if (!is_object($this->smarty->filter_handler) && (isset($this->smarty->autoload_filters['pre']) || isset($this->smarty->registered_filters['pre']) || isset($this->smarty->autoload_filters['post']) || isset($this->smarty->registered_filters['post']))) { - $this->smarty->loadPlugin('Smarty_Internal_Run_Filter'); - $this->smarty->filter_handler = new Smarty_Internal_Run_Filter; - } } /** diff --git a/libs/sysplugins/internal.run_filter.php b/libs/sysplugins/internal.run_filter.php index 71c67e2f..87235326 100644 --- a/libs/sysplugins/internal.run_filter.php +++ b/libs/sysplugins/internal.run_filter.php @@ -22,36 +22,38 @@ class Smarty_Internal_Run_Filter extends Smarty_Internal_Base { * plugin filename format: filtertype.filtername.php * Smarty2 filter plugins could be used * - * @param string $type the type of filter ('pre','post' or 'output') which shall run + * @param string $type the type of filter ('pre','post','output' or 'variable') which shall run * @param string $content the content which shall be processed by the filters * @return string the filtered content */ - public function execute($type, $content) + public function execute($type, $content, $flag = null) { - $output = $content; - // loop over autoload filters of specified type - if (!empty($this->smarty->autoload_filters[$type])) { - foreach ((array)$this->smarty->autoload_filters[$type] as $name) { - $plugin_name = "Smarty_{$type}filter_{$name}"; - if ($this->smarty->loadPlugin($plugin_name)) { - // use class plugin if found - if (class_exists($plugin_name, false)) { - // loaded class of filter plugin - $output = call_user_func_array(array($plugin_name, 'execute'), array($output, $this->smarty)); - } elseif (function_exists($plugin_name)) { - // use loaded Smarty2 style plugin - $output = call_user_func_array($plugin_name, array($output, $this->smarty)); + $output = $content; + if ($type != 'variable' || ($this->smarty->variable_filter && $flag !== false) || $flag === true) { + // loop over autoload filters of specified type + if (!empty($this->smarty->autoload_filters[$type])) { + foreach ((array)$this->smarty->autoload_filters[$type] as $name) { + $plugin_name = "Smarty_{$type}filter_{$name}"; + if ($this->smarty->loadPlugin($plugin_name)) { + // use class plugin if found + if (class_exists($plugin_name, false)) { + // loaded class of filter plugin + $output = call_user_func_array(array($plugin_name, 'execute'), array($output, $this->smarty)); + } elseif (function_exists($plugin_name)) { + // use loaded Smarty2 style plugin + $output = call_user_func_array($plugin_name, array($output, $this->smarty)); + } + } else { + // nothing found, throw exception + throw new Exception("Unable to load filter {$plugin_name}"); } - } else { - // nothing found, throw exception - throw new Exception("Unable to load filter {$plugin_name}"); } } - } - // loop over registerd filters of specified type - if (!empty($this->smarty->registered_filters[$type])) { - foreach ($this->smarty->registered_filters[$type] as $key => $name) { - $output = call_user_func_array($this->smarty->registered_filters[$type][$key], array($output, $this->smarty)); + // loop over registerd filters of specified type + if (!empty($this->smarty->registered_filters[$type])) { + foreach ($this->smarty->registered_filters[$type] as $key => $name) { + $output = call_user_func_array($this->smarty->registered_filters[$type][$key], array($output, $this->smarty)); + } } } // return filtered output diff --git a/libs/sysplugins/internal.template.php b/libs/sysplugins/internal.template.php index c3df08bb..38db5396 100644 --- a/libs/sysplugins/internal.template.php +++ b/libs/sysplugins/internal.template.php @@ -91,11 +91,6 @@ class Smarty_Internal_Template extends Smarty_Internal_TemplateBase { $this->cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($this->caching_type); $this->parent = $_parent; $this->tpl_vars['smarty'] = new Smarty_Variable; - // load filter handler if required - if (!is_object($this->smarty->filter_handler) && (isset($this->smarty->autoload_filters['output']) || isset($this->smarty->registered_filters['output']))) { - $this->smarty->loadPlugin('Smarty_Internal_Run_Filter'); - $this->smarty->filter_handler = new Smarty_Internal_Run_Filter; - } // Template resource $this->template_resource = $template_resource; // parse resource name diff --git a/libs/sysplugins/method.disablevariablefilter.php b/libs/sysplugins/method.disablevariablefilter.php new file mode 100644 index 00000000..45aef81e --- /dev/null +++ b/libs/sysplugins/method.disablevariablefilter.php @@ -0,0 +1,26 @@ +smarty->variable_filter = false; + return; + } +} + +?> diff --git a/libs/sysplugins/method.enablevariablefilter.php b/libs/sysplugins/method.enablevariablefilter.php new file mode 100644 index 00000000..cd5f58aa --- /dev/null +++ b/libs/sysplugins/method.enablevariablefilter.php @@ -0,0 +1,26 @@ +smarty->variable_filter = true; + return; + } +} + +?> diff --git a/libs/sysplugins/method.getvariablefilter.php b/libs/sysplugins/method.getvariablefilter.php new file mode 100644 index 00000000..7613fd7e --- /dev/null +++ b/libs/sysplugins/method.getvariablefilter.php @@ -0,0 +1,25 @@ +smarty->variable_filter; + } +} + +?> diff --git a/libs/sysplugins/method.load_filter.php b/libs/sysplugins/method.load_filter.php index 1713cebc..a0e4ee99 100644 --- a/libs/sysplugins/method.load_filter.php +++ b/libs/sysplugins/method.load_filter.php @@ -25,12 +25,12 @@ class Smarty_Method_Load_Filter extends Smarty_Internal_Base { */ function execute($type, $name) { - $_plugin = "Smarty_{$type}filter_{$name}"; + $_plugin = "smarty_{$type}filter_{$name}"; $_filter_name = $_plugin; if ($this->smarty->loadPlugin($_plugin)) { if (class_exists($_plugin, false)) { $_plugin = array($_plugin, 'execute'); - } + } if (is_callable($_plugin)) { $this->smarty->registered_filters[$type][$_filter_name] = $_plugin; return; diff --git a/libs/sysplugins/method.register_variablefilter.php b/libs/sysplugins/method.register_variablefilter.php new file mode 100644 index 00000000..bd38cbcb --- /dev/null +++ b/libs/sysplugins/method.register_variablefilter.php @@ -0,0 +1,33 @@ +smarty->registered_filters['variable'][$_name] = $function; + } +} + +?> diff --git a/libs/sysplugins/method.unregister_outputfilter.php b/libs/sysplugins/method.unregister_outputfilter.php index 7d120253..7b477b96 100644 --- a/libs/sysplugins/method.unregister_outputfilter.php +++ b/libs/sysplugins/method.unregister_outputfilter.php @@ -25,7 +25,8 @@ class Smarty_Method_Unregister_Outputfilter extends Smarty_Internal_Base { */ public function execute($function) { - unset($this->smarty->registered_filters['output'][$function]); + $_name = (is_array($function)) ? $function[0] : $function; + unset($this->smarty->registered_filters['output'][$_name]); } } diff --git a/libs/sysplugins/method.unregister_postfilter.php b/libs/sysplugins/method.unregister_postfilter.php index 157be67e..98b254fc 100644 --- a/libs/sysplugins/method.unregister_postfilter.php +++ b/libs/sysplugins/method.unregister_postfilter.php @@ -24,7 +24,8 @@ class Smarty_Method_Unregister_Postfilter extends Smarty_Internal_Base { */ public function execute($function) { - unset($this->smarty->registered_filters['post'][$function]); + $_name = (is_array($function)) ? $function[0] : $function; + unset($this->smarty->registered_filters['post'][$_name]); } } diff --git a/libs/sysplugins/method.unregister_prefilter.php b/libs/sysplugins/method.unregister_prefilter.php index e7bbc9fa..bfab647f 100644 --- a/libs/sysplugins/method.unregister_prefilter.php +++ b/libs/sysplugins/method.unregister_prefilter.php @@ -24,7 +24,8 @@ class Smarty_Method_Unregister_Prefilter extends Smarty_Internal_Base { */ public function execute($function) { - unset($this->smarty->registered_filters['pre'][$function]); + $_name = (is_array($function)) ? $function[0] : $function; + unset($this->smarty->registered_filters['pre'][$_name]); } } diff --git a/libs/sysplugins/method.unregister_variablefilter.php b/libs/sysplugins/method.unregister_variablefilter.php new file mode 100644 index 00000000..8fd4a3f7 --- /dev/null +++ b/libs/sysplugins/method.unregister_variablefilter.php @@ -0,0 +1,32 @@ +smarty->registered_filters['variable'][$_name]); + } +} + +?>