update math function with format attribute

This commit is contained in:
mohrt
2001-03-11 21:20:14 +00:00
parent 354dba0f17
commit 9a3dcbe1cd
7 changed files with 143 additions and 70 deletions

7
NEWS
View File

@@ -1,8 +1,13 @@
- 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
-------------

View File

@@ -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,14 +433,60 @@ function smarty_func_math() {
function smarty_func_fetch() {
extract(func_get_arg(0));
if (empty($file)) {
if(empty($file)) {
trigger_error("parameter 'file' cannot be empty");
return;
}
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: */
?>

View File

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

View File

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

View File

@@ -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
</programlisting>
</example>
</sect2>

View File

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

View File

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