fixed literal tags and other optional delimiters

This commit is contained in:
mohrt
2001-03-01 15:32:56 +00:00
parent ea1f3ded5a
commit 499f995085
6 changed files with 108 additions and 14 deletions

View File

@@ -273,6 +273,53 @@ function smarty_func_html_select_date()
print $html_result; 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: */ /* vim: set expandtab: */
?> ?>

View File

@@ -85,7 +85,8 @@ class Smarty
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options', 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', 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); preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_contents, $match);
$this->_literal_blocks = $match[1]; $this->_literal_blocks = $match[1];
$template_contents = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $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. */ /* Gather all template tags. */
preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_contents, $match); preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_contents, $match);
@@ -1350,6 +1351,14 @@ class Smarty
return true; return true;
} }
/*======================================================================*\
Function: quote_replace
Purpose: Quote subpattern references
\*======================================================================*/
function quote_replace($string)
{
return preg_replace('![\\$]\d!', '\\\\\\0', $string);
}
/*======================================================================*\ /*======================================================================*\
Function: _set_error_msg() Function: _set_error_msg()

View File

@@ -694,7 +694,7 @@ $smarty->display("index.tpl");
</example> </example>
<para> <para>
You can also pass a cache id as an an optional second parameter 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.
</para> </para>
<example> <example>
<title>is_cached with multiple-cache template</title> <title>is_cached with multiple-cache template</title>
@@ -1566,7 +1566,7 @@ e-mail: jane@mydomain.com&lt;p&gt;
<title>sectionelse</title> <title>sectionelse</title>
<programlisting> <programlisting>
{* 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} {section name=customer loop=$custid}
id: {$customer/custid}&lt;br&gt; id: {$customer/custid}&lt;br&gt;
{sectionelse} {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.
IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE. IF STRIKE ISN'T SETTLED QUICKLY IT MAY LAST A WHILE.
</programlisting>
</example>
</sect2>
<sect2>
<title>Combining Modifiers</title>
<para>
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.
</para>
<example>
<title>combining modifiers</title>
<programlisting>
{$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. . .
</programlisting> </programlisting>
</example> </example>
</sect2> </sect2>
@@ -2829,8 +2858,8 @@ function makeTimeStamp($year="",$month="",$day="")
added other features too numerous to mention. added other features too numerous to mention.
</para> </para>
<para> <para>
Anne Holz &lt;anne@ispi.net&gt;: Many of Smarty's formatting features were Anne Holz &lt;anne@ispi.net&gt;: Provided creative input with respect
a direct result of needs from her department. to web design.
</para> </para>
<para> <para>
Frank Kromann &lt;fmk@php.net&gt;: Idea of custom function ability. Frank Kromann &lt;fmk@php.net&gt;: Idea of custom function ability.

View File

@@ -85,7 +85,8 @@ class Smarty
var $custom_funcs = array( 'html_options' => 'smarty_func_html_options', 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', 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); preg_match_all("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $template_contents, $match);
$this->_literal_blocks = $match[1]; $this->_literal_blocks = $match[1];
$template_contents = preg_replace("!{$ldq}literal{$rdq}(.*?){$ldq}/literal{$rdq}!s", $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. */ /* Gather all template tags. */
preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_contents, $match); preg_match_all("!{$ldq}\s*(.*?)\s*{$rdq}!s", $template_contents, $match);
@@ -1350,6 +1351,14 @@ class Smarty
return true; return true;
} }
/*======================================================================*\
Function: quote_replace
Purpose: Quote subpattern references
\*======================================================================*/
function quote_replace($string)
{
return preg_replace('![\\$]\d!', '\\\\\\0', $string);
}
/*======================================================================*\ /*======================================================================*\
Function: _set_error_msg() Function: _set_error_msg()