Get Smarty

Donate

Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Advertisement

{math}

{math}可以让模板设计者在模板中进行一些数学运算。

  • 任何在模板中的数字变量都可以进行计算,而计算的结果会替代原来的标签。

  • 模板变量或者静态的值都可以作为参数进行计算。

  • 可以允许的计算有 +, -, /, *, abs, ceil, cos, exp, floor, log, log10, max, min, pi, pow, rand, round, sin, sqrt, srans 和 tan 。 参见PHP手册,了解math 函数。

  • 如果你提供了assign 属性, {math}函数的输出将不会显示,而是赋值给模板变量。

技术说明

因为使用了PHP函数 eval(),所以{math}是一个比较耗费性能的操作。 在PHP中做数学运算会更高效,所以尽可能在PHP进行数学运算并且把结果 assign()到模板中。 比较不建议的做法是在循环中使用{math}函数,比如在 {section}循环。

参数名称 类型 必选参数 默认值 说明
equation string Yes n/a 数学运算式
format string No n/a 结果的显示格式(sprintf)
var numeric Yes n/a 运算的变量值
assign string No n/a 用于赋值的变量名
[var ...] numeric Yes n/a 运算的变量值

Example 8.21. {math}

例子 a:


   {* $height=4, $width=5 *}

   {math equation="x + y" x=$height y=$width}

  

输出:


   9

  

例子 b:


   {* $row_height = 10, $row_width = 20, #col_div# = 2, assigned in template *}

   {math equation="height * width / division"
   height=$row_height
   width=$row_width
   division=#col_div#}

  

输出:


   100

  

例子 c:


   {* you can use parenthesis *}

   {math equation="(( x + y ) / z )" x=2 y=10 z=2}

  

输出:


   6

  

例子 d:


   {* you can supply a format parameter in sprintf format *}

   {math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}
   
  

输出:


   9.44

  

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors