From 2a90f7392a82de9a757467be8fdba97ec9d2a721 Mon Sep 17 00:00:00 2001 From: andrey Date: Fri, 2 Feb 2001 16:55:55 +0000 Subject: [PATCH] See changelog. --- NEWS | 5 +++++ Smarty.class.php | 44 ++++++++++++++++++++++++++++++++-------- demo/templates/index.tpl | 5 +++-- libs/Smarty.class.php | 44 ++++++++++++++++++++++++++++++++-------- templates/index.tpl | 5 +++-- 5 files changed, 83 insertions(+), 20 deletions(-) diff --git a/NEWS b/NEWS index b8b24e2d..e2bb8f2b 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,8 @@ + - made Smarty catch unimplemented modifiers and custom functions and output + error messages during compilation instead of failing during run time. + (Andrei) + - added register_custom_function() and register_modifier() API calls + to make registering stuff easier. (Andrei) - added template results caching capability. (Monte, Andrei) - added optional 'options' attribute to html_options custom function that allows passing associative arrays for values/output. (Andrei) diff --git a/Smarty.class.php b/Smarty.class.php index 7199fadf..dd5f9f55 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -69,7 +69,7 @@ class Smarty // 0 = never expires. default is one hour (3600) - var $tpl_file_ext = ".tpl"; // template file extentions + var $tpl_file_ext = ".tpl"; // template files extention var $allow_php = false; // whether or not to allow embedded php // in the templates. By default, php tags @@ -170,6 +170,27 @@ class Smarty unset($this->_tpl_vars[$tpl_var]); } + +/*======================================================================*\ + Function: register_custom_function + Purpose: Registers custom function to be used in templates +\*======================================================================*/ + function register_custom_function($function, $function_impl) + { + $this->custom_funcs[$function] = $function_impl; + } + + +/*======================================================================*\ + Function: register_modifier + Purpose: Registers modifier to be used in templates +\*======================================================================*/ + function register_modifier($modifier, $modifier_impl) + { + $this->custom_mods[$modifier] = $modifier_impl; + } + + /*======================================================================*\ Function: clear_cache() Purpose: clear all cached template files for given template @@ -491,7 +512,7 @@ class Smarty \*======================================================================*/ function _process_cached_inserts($results) { - preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'!Uis', + preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'\n??!Uis', $results, $match); list($cached_inserts, $insert_args) = $match; @@ -499,10 +520,6 @@ class Smarty $attrs = $this->_parse_attrs($insert_args[$i], false); $name = $this->_dequote($attrs['name']); - if (empty($name)) { - $this->_syntax_error("missing insert name"); - } - $arg_list = array(); foreach ($attrs as $arg_name => $arg_value) { if ($arg_name == 'name') continue; @@ -600,15 +617,21 @@ class Smarty return $this->_compile_custom_tag($tag_command, $tag_args); } else { $this->_syntax_error("unknown tag - '$tag_command'", E_USER_WARNING); - return ""; + return; } } } function _compile_custom_tag($tag_command, $tag_args) { - $attrs = $this->_parse_attrs($tag_args); $function = $this->custom_funcs[$tag_command]; + + if (!function_exists($function)) { + $this->_syntax_error("custom function '$tag_command' is not implemented", E_USER_WARNING); + return; + } + + $attrs = $this->_parse_attrs($tag_args); foreach ($attrs as $arg_name => $arg_value) { if (is_bool($arg_value)) $arg_value = $arg_value ? 'true' : 'false'; @@ -1104,6 +1127,11 @@ class Smarty if (!isset($mod_func_name)) $mod_func_name = $modifier_name; + if (!function_exists($mod_func_name)) { + $this->_syntax_error("modifier '$modifier_name' is not implemented", E_USER_WARNING); + continue; + } + $this->_parse_vars_props($modifier_args); if (count($modifier_args) > 0) diff --git a/demo/templates/index.tpl b/demo/templates/index.tpl index aa01ba3e..d5ffed3b 100644 --- a/demo/templates/index.tpl +++ b/demo/templates/index.tpl @@ -47,7 +47,8 @@ testing strip tags +{insert name = foo + arg1=5} + test: {$now|date_format:"%I:%M %p"} -{insert name = foo arg1=5} - diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 7199fadf..dd5f9f55 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -69,7 +69,7 @@ class Smarty // 0 = never expires. default is one hour (3600) - var $tpl_file_ext = ".tpl"; // template file extentions + var $tpl_file_ext = ".tpl"; // template files extention var $allow_php = false; // whether or not to allow embedded php // in the templates. By default, php tags @@ -170,6 +170,27 @@ class Smarty unset($this->_tpl_vars[$tpl_var]); } + +/*======================================================================*\ + Function: register_custom_function + Purpose: Registers custom function to be used in templates +\*======================================================================*/ + function register_custom_function($function, $function_impl) + { + $this->custom_funcs[$function] = $function_impl; + } + + +/*======================================================================*\ + Function: register_modifier + Purpose: Registers modifier to be used in templates +\*======================================================================*/ + function register_modifier($modifier, $modifier_impl) + { + $this->custom_mods[$modifier] = $modifier_impl; + } + + /*======================================================================*\ Function: clear_cache() Purpose: clear all cached template files for given template @@ -491,7 +512,7 @@ class Smarty \*======================================================================*/ function _process_cached_inserts($results) { - preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'!Uis', + preg_match_all('!'.$this->_smarty_md5.'{insert_cache (.*)}'.$this->_smarty_md5.'\n??!Uis', $results, $match); list($cached_inserts, $insert_args) = $match; @@ -499,10 +520,6 @@ class Smarty $attrs = $this->_parse_attrs($insert_args[$i], false); $name = $this->_dequote($attrs['name']); - if (empty($name)) { - $this->_syntax_error("missing insert name"); - } - $arg_list = array(); foreach ($attrs as $arg_name => $arg_value) { if ($arg_name == 'name') continue; @@ -600,15 +617,21 @@ class Smarty return $this->_compile_custom_tag($tag_command, $tag_args); } else { $this->_syntax_error("unknown tag - '$tag_command'", E_USER_WARNING); - return ""; + return; } } } function _compile_custom_tag($tag_command, $tag_args) { - $attrs = $this->_parse_attrs($tag_args); $function = $this->custom_funcs[$tag_command]; + + if (!function_exists($function)) { + $this->_syntax_error("custom function '$tag_command' is not implemented", E_USER_WARNING); + return; + } + + $attrs = $this->_parse_attrs($tag_args); foreach ($attrs as $arg_name => $arg_value) { if (is_bool($arg_value)) $arg_value = $arg_value ? 'true' : 'false'; @@ -1104,6 +1127,11 @@ class Smarty if (!isset($mod_func_name)) $mod_func_name = $modifier_name; + if (!function_exists($mod_func_name)) { + $this->_syntax_error("modifier '$modifier_name' is not implemented", E_USER_WARNING); + continue; + } + $this->_parse_vars_props($modifier_args); if (count($modifier_args) > 0) diff --git a/templates/index.tpl b/templates/index.tpl index aa01ba3e..d5ffed3b 100644 --- a/templates/index.tpl +++ b/templates/index.tpl @@ -47,7 +47,8 @@ testing strip tags +{insert name = foo + arg1=5} + test: {$now|date_format:"%I:%M %p"} -{insert name = foo arg1=5} -