mirror of
https://github.com/smarty-php/smarty.git
synced 2025-08-07 03:44:26 +02:00
update math function with format attribute
This commit is contained in:
7
NEWS
7
NEWS
@@ -1,8 +1,13 @@
|
|||||||
|
- added format attribute to math function (Monte)
|
||||||
- added html_select_time custom function. (Andrei)
|
- 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
|
Version 1.3.1pl1
|
||||||
--------------
|
--------------
|
||||||
- bug fix, recovered missing _syntax_error function
|
- bug fix, recovered missing _syntax_error function (Monte)
|
||||||
|
|
||||||
Version 1.3.1
|
Version 1.3.1
|
||||||
-------------
|
-------------
|
||||||
|
@@ -404,7 +404,7 @@ function smarty_func_math() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach($args as $key => $val) {
|
foreach($args as $key => $val) {
|
||||||
if($key != "equation") {
|
if($key != "equation" && $key != "format") {
|
||||||
// make sure value is not empty
|
// make sure value is not empty
|
||||||
if(strlen($val)==0) {
|
if(strlen($val)==0) {
|
||||||
trigger_error("math: parameter $key is empty");
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*======================================================================*\
|
/*======================================================================*\
|
||||||
@@ -432,10 +437,56 @@ function smarty_func_fetch() {
|
|||||||
trigger_error("parameter 'file' cannot be empty");
|
trigger_error("parameter 'file' cannot be empty");
|
||||||
return;
|
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: */
|
/* vim: set expandtab: */
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -100,7 +100,11 @@ class Smarty
|
|||||||
'string_format' => 'smarty_mod_string_format',
|
'string_format' => 'smarty_mod_string_format',
|
||||||
'replace' => 'smarty_mod_replace',
|
'replace' => 'smarty_mod_replace',
|
||||||
'strip_tags' => 'smarty_mod_strip_tags',
|
'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
|
// internal vars
|
||||||
|
@@ -463,7 +463,7 @@ class Smarty_Compiler extends Smarty {
|
|||||||
$this->_syntax_error("missing section name");
|
$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']";
|
$section_props = "\$_sections[$section_name]['properties']";
|
||||||
|
|
||||||
foreach ($attrs as $attr_name => $attr_value) {
|
foreach ($attrs as $attr_name => $attr_value) {
|
||||||
|
@@ -2356,6 +2356,15 @@ OUTPUT:
|
|||||||
|
|
||||||
6
|
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>
|
</programlisting>
|
||||||
</example>
|
</example>
|
||||||
</sect2>
|
</sect2>
|
||||||
|
@@ -100,7 +100,11 @@ class Smarty
|
|||||||
'string_format' => 'smarty_mod_string_format',
|
'string_format' => 'smarty_mod_string_format',
|
||||||
'replace' => 'smarty_mod_replace',
|
'replace' => 'smarty_mod_replace',
|
||||||
'strip_tags' => 'smarty_mod_strip_tags',
|
'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
|
// internal vars
|
||||||
|
@@ -463,7 +463,7 @@ class Smarty_Compiler extends Smarty {
|
|||||||
$this->_syntax_error("missing section name");
|
$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']";
|
$section_props = "\$_sections[$section_name]['properties']";
|
||||||
|
|
||||||
foreach ($attrs as $attr_name => $attr_value) {
|
foreach ($attrs as $attr_name => $attr_value) {
|
||||||
|
Reference in New Issue
Block a user