What if you want to generate random numbers in the smarty template? Generated in the PHP file and assigned to the template. This is certainly feasible, but too complex, and the following authors share a little trick of how to generate random numbers directly in the Smarty template.
Now suppose that you need to generate a random number between 125-324 in the Smarty template, you can do this as follows:
{Math Equation=rand (125,324)}
That's what we're going to do. is not very simple, in fact, the main use of the Smarty template in the math function , the following smarty math function and usage to do a brief explanation .
Math allows the template designer to perform mathematical expression operations in the template. A variable of any numeric type can be used in an expression, and the result is output at the location of the math tag. The variables used in the expression are passed as arguments to the function, which can be either template variables or static values. The currently available operators are: +,-,/, *, ABS, ceil, COS, exp, floor, log, log10, max, Min, pi, pow, rand, round, sin, sqrt, Srans, and Tan. About For more information on mathematical functions, see the PHP documentation.
If the special attribute "Assign" is specified, the output value of the function is assigned to the template variable specified by assign instead of the direct output.
However, it is important to note that the math function is inefficient because of the use of the Eval () function of PHP. Doing math in PHP is more efficient, so do the math in PHP as much as possible and assign the results to template variables.
Smarty Math Function Demo
{* $height =4, $width = 5 *}
{Math equation= "x + y" x= $height y= $width}
Output results: 9
{* $row _height = ten, $row _width = #col_div # = 2, assigned in template *}
{Math equation= "Height * width/division"
height= $row _height
Width= $row _width
division= #col_div #}
Output results: 100
{* Can use parenthesis http://www.phpernote.com/tag/smarty*}
{Math equation= "((x + y)/z)" X=2 y=10 z=2}
Output results: 6
{* You can supply a format parameter in sprintf format *}
{Math equation= "x + y" x=4.4444 y=5.0000 format= "%.2f"}
Output results: 9.44
Articles you may be interested in
- Add latest tags to information in smarty templates
- Smarty method for determining the empty array in a template
- Use PHP functions in Smarty Templates and how to use multiple functions for a variable in a smarty template
- Smarty extension for loop in template
- TD in Smarty template with circular form complement
- Smarty Template Retention Variables Summary
- How constants defined by define in the program are used in the Smarty template
- Smarty Template Variable operator summary
http://www.bkjia.com/PHPjc/764130.html www.bkjia.com true http://www.bkjia.com/PHPjc/764130.html techarticle What if you want to generate random numbers in the smarty template? Generated in the PHP file and assigned to the template. This is certainly feasible, but too complex, the following author how ...