- implementation of a 'variable' filter as replacement for default modifier

- update of unregister_....filter methods
This commit is contained in:
Uwe.Tews
2009-04-10 01:15:53 +00:00
parent a424da0671
commit 066467ca82
16 changed files with 221 additions and 50 deletions

View File

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

View File

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

View File

@@ -0,0 +1,21 @@
<?php
/**
* Smarty plugin
*
* @package Smarty
* @subpackage PluginsFilter
*/
/**
* Smarty htmlspecialchars variablefilter plugin
*
* @param string $source input string
* @param object $ &$smarty Smarty object
* @return string filtered output
*/
function smarty_variablefilter_htmlspecialchars($source, &$smarty)
{
return htmlspecialchars($source, ENT_QUOTES);
}
?>

View File

@@ -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 = '<?php $_smarty_tpl->assign(' . $_attr['assign'] . ',' . $_attr['value'] . ');?>';
} else {
// display value
$this->compiler->has_output = true;
$output = '<?php echo ' . $_attr['value'] . ';?>';
$output = '<?php echo $this->smarty->filter_handler->execute(\'variable\', ' . $_attr['value'] . ',' . $_attr['filter'] . ');?>';
}
return $output;
}

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,26 @@
<?php
/**
* Smarty method disableVariableFilter
*
* Disable filter on variable output
*
* @package Smarty
* @subpackage SmartyMethod
* @author Uwe Tews
*/
/**
* Smarty class disableVariableFilter
*
* Disable filter on variable output
*/
class Smarty_Method_disableVariableFilter extends Smarty_Internal_Base {
public function execute()
{
$this->smarty->variable_filter = false;
return;
}
}
?>

View File

@@ -0,0 +1,26 @@
<?php
/**
* Smarty method enableVariableFilter
*
* Enable filter on variable output
*
* @package Smarty
* @subpackage SmartyMethod
* @author Uwe Tews
*/
/**
* Smarty class enableVariableFilter
*
* Enable filter on variable output
*/
class Smarty_Method_enableVariableFilter extends Smarty_Internal_Base {
public function execute()
{
$this->smarty->variable_filter = true;
return;
}
}
?>

View File

@@ -0,0 +1,25 @@
<?php
/**
* Smarty method getVariableFilter
*
* get status of filter on variable output
*
* @package Smarty
* @subpackage SmartyMethod
* @author Uwe Tews
*/
/**
* Smarty class getVariableFilter
*
* get status of filter on variable output
*/
class Smarty_Method_getVariableFilter extends Smarty_Internal_Base {
public function execute()
{
return $this->smarty->variable_filter;
}
}
?>

View File

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

View File

@@ -0,0 +1,33 @@
<?php
/**
* Smarty method Register_Variablefilter
*
* Registers a PHP function as an output filter for variables
*
* @package Smarty
* @subpackage SmartyMethod
* @author Uwe Tews
*/
/**
* Smarty class Register_Variablefilter
*
* Register a PHP function as variablefilter
*/
class Smarty_Method_Register_Variablefilter extends Smarty_Internal_Base {
/**
* Registers an output filter function which
* runs over any variable output
*
* @param callback $function
*/
public function execute($function)
{
$_name = (is_array($function)) ? $function[0] : $function;
$this->smarty->registered_filters['variable'][$_name] = $function;
}
}
?>

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,32 @@
<?php
/**
* Smarty method Unregister_Variablefilter
*
* Unregister a variablefilter
*
* @package Smarty
* @subpackage SmartyMethod
* @author Uwe Tews
*/
/**
* Smarty class Unregister_Variablefilter
*
* Unregister a variablefilter
*/
class Smarty_Method_Unregister_Variablefilter extends Smarty_Internal_Base {
/**
* Unregisters a variablefilter function
*
* @param callback $function
*/
public function execute($function)
{
$_name = (is_array($function)) ? $function[0] : $function;
unset($this->smarty->registered_filters['variable'][$_name]);
}
}
?>