What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
{math}
を使用すると、
テンプレートのデザイナーがテンプレート内で数学の計算を実行できます。
式の中では、数値型のテンプレート変数を使用でき、結果はタグの位置に出力されます。
式で使用する変数はパラメータとして渡します。 これはテンプレート変数あるいは静的な値のいずれかとなります。
+, -, /, *, abs, ceil, cos, exp, floor, log, log10, max, min, pi, pow, rand, round, sin, sqrt, srans および tan を使用できます。 これらの詳細については、PHP の 数学 関数のマニュアルを参照してください。
assign
属性を指定すると、
{math}
関数の出力はテンプレート変数に格納され、
テンプレートには出力されません。
{math}
は PHP の
eval()
関数を使用するのでパフォーマンス的にコストの高い関数です。
PHP 内で math 関数を実行する事は、テンプレートで行うよりもはるかに効率的で、
mathの計算がPHPで可能な場合はPHPで行い、結果をテンプレートに
assign()
するようにしましょう。
{section}
ループ内のような反復動作で
{math}
関数を呼び出す事は避けて下さい。
属性名 | 型 | 必須 | デフォルト | 概要 |
---|---|---|---|---|
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, テンプレートで割り当てます *} {math equation="height * width / division" height=$row_height width=$row_width division=#col_div#}
上の例の出力
100
サンプル c:
{* 括弧も使用できます *} {math equation="(( x + y ) / z )" x=2 y=10 z=2}
上の例の出力
6
サンプル d:
{* sprintf 形式のフォーマット文字列を指定できます *} {math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}
上の例の出力
9.44