From 499f9950851c1fb5c4bd799a5d2f876f820c935e Mon Sep 17 00:00:00 2001 From: mohrt Date: Thu, 1 Mar 2001 15:32:56 +0000 Subject: [PATCH] fixed literal tags and other optional delimiters --- Smarty.addons.php | 47 +++++++++++++++++++++++++++++++++++++++++++ Smarty.class.php | 15 +++++++++++--- demo/index.php | 4 ++-- docs.sgml | 37 ++++++++++++++++++++++++++++++---- index.php | 4 ++-- libs/Smarty.class.php | 15 +++++++++++--- 6 files changed, 108 insertions(+), 14 deletions(-) diff --git a/Smarty.addons.php b/Smarty.addons.php index e5ef6ecd..fdb5aa4a 100644 --- a/Smarty.addons.php +++ b/Smarty.addons.php @@ -273,6 +273,53 @@ function smarty_func_html_select_date() print $html_result; } +function smarty_func_math() { + $args=func_get_arg(0); + + // be sure equation parameter is present + if(empty($args["equation"])) { + trigger_error("math: missing equation parameter"); + return; + } + + $equation = $args["equation"]; + + // make sure parenthesis are balanced + if(substr_count($equation,"(") != substr_count($equation,")")) { + trigger_error("math: unbalanced parenthesis"); + return; + } + + // match all vars in equation, make sure all are passed + preg_match_all("![a-zA-Z][a-zA-Z0-9]*!",$equation,$match); + + foreach($match[0] as $curr_var) { + if(!in_array($curr_var,array_keys($args)) && + !in_array($curr_var,array('abs','ceil','cos','exp','floor','log','log10', + 'max','min','pi','pow','rand','round','sin','sqrt','srand','tan')) ) { + trigger_error("math: parameter $curr_var not passed as argument"); + return; + } + } + + foreach($args as $key => $val) { + if($key != "equation") { + // make sure value is not empty + if(strlen($val)==0) { + trigger_error("math: parameter $key is empty"); + return; + } + if(!is_numeric($val)) { + trigger_error("math: parameter $key: is not numeric"); + return; + } + $equation = preg_replace("/\b$key\b/",$val,$equation); + } + } + + eval("echo ".$equation.";"); +} + /* vim: set expandtab: */ ?> diff --git a/Smarty.class.php b/Smarty.class.php index 76039dee..4215604e 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -85,7 +85,8 @@ class Smarty var $custom_funcs = array( 'html_options' => 'smarty_func_html_options', - 'html_select_date' => 'smarty_func_html_select_date' + 'html_select_date' => 'smarty_func_html_select_date', + 'math' => 'smarty_func_math' ); var $custom_mods = array( 'lower' => 'strtolower', @@ -510,7 +511,7 @@ class Smarty preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_contents, $match); $this->_literal_blocks = $match[1]; $template_contents = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", - '{literal}', $template_contents); + $this->quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $template_contents); /* Gather all template tags. */ preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_contents, $match); @@ -712,7 +713,7 @@ class Smarty $arg_value = $arg_value ? 'true' : 'false'; $arg_list[] = "'$arg_name' => $arg_value"; } - + return ""; } @@ -1350,6 +1351,14 @@ class Smarty return true; } +/*======================================================================*\ + Function: quote_replace + Purpose: Quote subpattern references +\*======================================================================*/ + function quote_replace($string) + { + return preg_replace('![\\$]\d!', '\\\\\\0', $string); + } /*======================================================================*\ Function: _set_error_msg() diff --git a/demo/index.php b/demo/index.php index d784a5af..4282a870 100644 --- a/demo/index.php +++ b/demo/index.php @@ -10,10 +10,10 @@ $smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill"); $smarty->assign("FirstName",array("John","Mary","James","Henry")); $smarty->assign("LastName",array("Doe","Smith","Johnson","Case")); $smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"), - array("I", "J", "K", "L"), array("M", "N", "O", "P"))); + array("I", "J", "K", "L"), array("M", "N", "O", "P"))); $smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"), - array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); + array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); $smarty->assign("option_values", array("NY","NE","KS","IA","OK","TX")); $smarty->assign("option_output", array("New York","Nebraska","Kansas","Iowa","Oklahoma","Texas")); diff --git a/docs.sgml b/docs.sgml index e2f25357..9acf13f0 100644 --- a/docs.sgml +++ b/docs.sgml @@ -694,7 +694,7 @@ $smarty->display("index.tpl"); You can also pass a cache id as an an optional second parameter - in the case you want multiple caches for the given template. + in case you want multiple caches for the given template. is_cached with multiple-cache template @@ -1566,7 +1566,7 @@ e-mail: jane@mydomain.com<p> sectionelse -{* sectionelse will execute in the case there are no $custid values *} +{* sectionelse will execute if there are no $custid values *} {section name=customer loop=$custid} id: {$customer/custid}<br> {sectionelse} @@ -2639,6 +2639,35 @@ OUTPUT: If Strike isn't Settled Quickly it may Last a While. IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. + + + + + Combining Modifiers + + You can combine as many modifiers as you like on a single variable. + This allows the ability to combine the application of different + modifiers to a single variable. The modifiers will be applied to + the variable in the order they are combined, from left to right. + + +combining modifiers + + +{$articleTitle} +{$articleTitle|upper|spacify} +{$articleTitle|lower|spacify|truncate} +{$articleTitle|lower|truncate:30|spacify} +{$articleTitle|lower|spacify|truncate:30:". . ."} + +OUTPUT: + +Smokers are Productive, but Death Cuts Efficiency. +S M O K E R S A R E P R O D U C T I V E , B U T D E A T H C U T S E F F I C I E N C Y . +s m o k e r s a r e p r o d u c t i v e , b u t d e a t h c u t s... +s m o k e r s a r e p r o d u c t i v e , b u t . . . +s m o k e r s a r e p. . . + @@ -2829,8 +2858,8 @@ function makeTimeStamp($year="",$month="",$day="") added other features too numerous to mention. - Anne Holz <anne@ispi.net>: Many of Smarty's formatting features were - a direct result of needs from her department. + Anne Holz <anne@ispi.net>: Provided creative input with respect + to web design. Frank Kromann <fmk@php.net>: Idea of custom function ability. diff --git a/index.php b/index.php index d784a5af..4282a870 100644 --- a/index.php +++ b/index.php @@ -10,10 +10,10 @@ $smarty->assign("Name","Fred Irving Johnathan Bradley Peppergill"); $smarty->assign("FirstName",array("John","Mary","James","Henry")); $smarty->assign("LastName",array("Doe","Smith","Johnson","Case")); $smarty->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"), - array("I", "J", "K", "L"), array("M", "N", "O", "P"))); + array("I", "J", "K", "L"), array("M", "N", "O", "P"))); $smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"), - array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); + array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234"))); $smarty->assign("option_values", array("NY","NE","KS","IA","OK","TX")); $smarty->assign("option_output", array("New York","Nebraska","Kansas","Iowa","Oklahoma","Texas")); diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 76039dee..4215604e 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -85,7 +85,8 @@ class Smarty var $custom_funcs = array( 'html_options' => 'smarty_func_html_options', - 'html_select_date' => 'smarty_func_html_select_date' + 'html_select_date' => 'smarty_func_html_select_date', + 'math' => 'smarty_func_math' ); var $custom_mods = array( 'lower' => 'strtolower', @@ -510,7 +511,7 @@ class Smarty preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_contents, $match); $this->_literal_blocks = $match[1]; $template_contents = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", - '{literal}', $template_contents); + $this->quote_replace($this->left_delimiter.'literal'.$this->right_delimiter), $template_contents); /* Gather all template tags. */ preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_contents, $match); @@ -712,7 +713,7 @@ class Smarty $arg_value = $arg_value ? 'true' : 'false'; $arg_list[] = "'$arg_name' => $arg_value"; } - + return ""; } @@ -1350,6 +1351,14 @@ class Smarty return true; } +/*======================================================================*\ + Function: quote_replace + Purpose: Quote subpattern references +\*======================================================================*/ + function quote_replace($string) + { + return preg_replace('![\\$]\d!', '\\\\\\0', $string); + } /*======================================================================*\ Function: _set_error_msg()