From 9a3dcbe1cd0d7ff43df37fefd846d5e9d7fcfa54 Mon Sep 17 00:00:00 2001 From: mohrt Date: Sun, 11 Mar 2001 21:20:14 +0000 Subject: [PATCH] update math function with format attribute --- NEWS | 9 +- Smarty.addons.php | 179 +++++++++++++++++++++------------ Smarty.class.php | 6 +- Smarty_Compiler.class.php | 2 +- docs.sgml | 9 ++ libs/Smarty.class.php | 6 +- libs/Smarty_Compiler.class.php | 2 +- 7 files changed, 143 insertions(+), 70 deletions(-) diff --git a/NEWS b/NEWS index a1629034..749959dd 100644 --- a/NEWS +++ b/NEWS @@ -1,8 +1,13 @@ - - added html_select_time custom function. (Andrei) + - added format attribute to math function (Monte) + - added html_select_time custom function. (Andrei) + - fixed minor PHP warning when attempting to unset an unset variable + (Monte) + - added count_characters, count_words, count_sentences, count_paragraphs + modifiers (Monte) Version 1.3.1pl1 -------------- - - bug fix, recovered missing _syntax_error function + - bug fix, recovered missing _syntax_error function (Monte) Version 1.3.1 ------------- diff --git a/Smarty.addons.php b/Smarty.addons.php index 47c32a29..66ff1618 100644 --- a/Smarty.addons.php +++ b/Smarty.addons.php @@ -267,8 +267,8 @@ function smarty_func_html_select_date() if ($display_days) { $days = range(1, 31); - for ($i = 0; $i < count($days); $i++) - $days[$i] = sprintf($day_format, $days[$i]); + for ($i = 0; $i < count($days); $i++) + $days[$i] = sprintf($day_format, $days[$i]); $html_result .= ''."\n"; - $html_result .= smarty_func_html_options(array('output' => $hours, - 'values' => $hours, - 'selected' => strftime($hour_fmt, $time), - 'print_result' => false)); + $html_result .= smarty_func_html_options(array('output' => $hours, + 'values' => $hours, + 'selected' => strftime($hour_fmt, $time), + 'print_result' => false)); $html_result .= "\n"; - } - - if ($display_minutes) { - $all_minutes = range(0, 59); - for ($i = 0; $i < count($all_minutes); $i+= $minute_interval) - $minutes[] = sprintf('%02d', $all_minutes[$i]); - $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval); + } + + if ($display_minutes) { + $all_minutes = range(0, 59); + for ($i = 0; $i < count($all_minutes); $i+= $minute_interval) + $minutes[] = sprintf('%02d', $all_minutes[$i]); + $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval); $html_result .= '\n"; - } - - if ($display_seconds) { - $all_seconds = range(0, 59); - for ($i = 0; $i < count($all_seconds); $i+= $second_interval) - $seconds[] = sprintf('%02d', $all_seconds[$i]); - $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval); + } + + if ($display_seconds) { + $all_seconds = range(0, 59); + for ($i = 0; $i < count($all_seconds); $i+= $second_interval) + $seconds[] = sprintf('%02d', $all_seconds[$i]); + $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval); $html_result .= '\n"; - } - - if ($display_meridian && !$use_24_hours) { - $html_result .= '\n"; - } - - print $html_result; + } + + if ($display_meridian && !$use_24_hours) { + $html_result .= '\n"; + } + + print $html_result; } @@ -404,7 +404,7 @@ function smarty_func_math() { } foreach($args as $key => $val) { - if($key != "equation") { + if($key != "equation" && $key != "format") { // make sure value is not empty if(strlen($val)==0) { trigger_error("math: parameter $key is empty"); @@ -418,7 +418,12 @@ function smarty_func_math() { } } - eval("echo ".$equation.";"); + eval("\$smarty_math_result = ".$equation.";"); + + if(empty($args["format"])) + echo $smarty_math_result; + else + printf($args["format"],$smarty_math_result); } /*======================================================================*\ @@ -428,12 +433,58 @@ function smarty_func_math() { function smarty_func_fetch() { extract(func_get_arg(0)); - if (empty($file)) { - trigger_error("parameter 'file' cannot be empty"); - return; - } + if(empty($file)) { + trigger_error("parameter 'file' cannot be empty"); + return; + } + readfile($file); +} - readfile($file); +/*======================================================================*\ + Function: smarty_mod_count_characters + Purpose: count the number of characters in a text +\*======================================================================*/ +function smarty_mod_count_characters($string,$include_spaces=false) { + + if($include_spaces) + return(strlen($string)); + + return preg_match_all("/[^\s]/",$string,$match); +} + +/*======================================================================*\ + Function: smarty_mod_count_words + Purpose: count the number of words in a text +\*======================================================================*/ +function smarty_mod_count_words($string) { + + // split text by ' ',\r,\n,\f,\t + $split_array = preg_split("/\s+/",$string); + // count matches that contain alphanumerics + $word_count = preg_grep("/[a-zA-Z0-9]/",$split_array); + + return count($word_count); +} + +/*======================================================================*\ + Function: smarty_mod_count_sentences + Purpose: count the number of sentences in a text +\*======================================================================*/ +function smarty_mod_count_sentences($string,$include_spaces=false) { + + // find periods with a word before but not after. + return preg_match_all("/[^\s]\.(?!\w)/",$string,$match); + +} + +/*======================================================================*\ + Function: smarty_mod_count_paragraphs + Purpose: count the number of sentences in a text +\*======================================================================*/ +function smarty_mod_count_paragraphs($string,$include_spaces=false) { + + // count \r or \n characters + return count( preg_split("/[\r\n]+/",$string) ); } /* vim: set expandtab: */ diff --git a/Smarty.class.php b/Smarty.class.php index 8e925698..eed61a8d 100644 --- a/Smarty.class.php +++ b/Smarty.class.php @@ -100,7 +100,11 @@ class Smarty 'string_format' => 'smarty_mod_string_format', 'replace' => 'smarty_mod_replace', 'strip_tags' => 'smarty_mod_strip_tags', - 'default' => 'smarty_mod_default' + 'default' => 'smarty_mod_default', + 'count_characters' => 'smarty_mod_count_characters', + 'count_words' => 'smarty_mod_count_words', + 'count_sentences' => 'smarty_mod_count_sentences', + 'count_paragraphs' => 'smarty_mod_count_paragraphs' ); // internal vars diff --git a/Smarty_Compiler.class.php b/Smarty_Compiler.class.php index 49124f42..b48c55fc 100644 --- a/Smarty_Compiler.class.php +++ b/Smarty_Compiler.class.php @@ -463,7 +463,7 @@ class Smarty_Compiler extends Smarty { $this->_syntax_error("missing section name"); } - $output .= "unset(\$_sections[$section_name]);\n"; + $output .= "if(isset(\$_sections[$section_name])) { unset(\$_sections[$section_name]); }\n"; $section_props = "\$_sections[$section_name]['properties']"; foreach ($attrs as $attr_name => $attr_value) { diff --git a/docs.sgml b/docs.sgml index d90a9501..dfaa580c 100644 --- a/docs.sgml +++ b/docs.sgml @@ -2356,6 +2356,15 @@ OUTPUT: 6 + +{* you can supply a format paramter in sprintf format *} + +{math equation="x + y" x=4.4444 y=5.0000 format="%.2f"} + +OUTPUT: + +9.44 + diff --git a/libs/Smarty.class.php b/libs/Smarty.class.php index 8e925698..eed61a8d 100644 --- a/libs/Smarty.class.php +++ b/libs/Smarty.class.php @@ -100,7 +100,11 @@ class Smarty 'string_format' => 'smarty_mod_string_format', 'replace' => 'smarty_mod_replace', 'strip_tags' => 'smarty_mod_strip_tags', - 'default' => 'smarty_mod_default' + 'default' => 'smarty_mod_default', + 'count_characters' => 'smarty_mod_count_characters', + 'count_words' => 'smarty_mod_count_words', + 'count_sentences' => 'smarty_mod_count_sentences', + 'count_paragraphs' => 'smarty_mod_count_paragraphs' ); // internal vars diff --git a/libs/Smarty_Compiler.class.php b/libs/Smarty_Compiler.class.php index 49124f42..b48c55fc 100644 --- a/libs/Smarty_Compiler.class.php +++ b/libs/Smarty_Compiler.class.php @@ -463,7 +463,7 @@ class Smarty_Compiler extends Smarty { $this->_syntax_error("missing section name"); } - $output .= "unset(\$_sections[$section_name]);\n"; + $output .= "if(isset(\$_sections[$section_name])) { unset(\$_sections[$section_name]); }\n"; $section_props = "\$_sections[$section_name]['properties']"; foreach ($attrs as $attr_name => $attr_value) {