mirror of
https://github.com/smarty-php/smarty.git
synced 2025-11-10 01:01:45 +01:00
205 lines
5.4 KiB
XML
205 lines
5.4 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 2586 Maintainer: takagi Status: ready -->
|
|
<!-- CREDITS: mat-sh,daichi,joe -->
|
|
<sect1 id="language.function.math">
|
|
<title>{math}</title>
|
|
<para>
|
|
<varname>{math}</varname> を使用すると、
|
|
テンプレートのデザイナーがテンプレート内で数学の計算を実行できます。
|
|
</para>
|
|
<itemizedlist>
|
|
<listitem><para>
|
|
式の中では、数値型のテンプレート変数を使用でき、結果はタグの位置に出力されます。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
式で使用する変数はパラメータとして渡します。
|
|
これはテンプレート変数あるいは静的な値のいずれかとなります。
|
|
</para></listitem>
|
|
|
|
<listitem><para>+, -, /, *, abs, ceil, cos, exp, floor, log, log10, max, min,
|
|
pi, pow, rand, round, sin, sqrt, srans および tan を使用できます。
|
|
これらの詳細については、PHP の
|
|
<ulink url="&url.php-manual;eval">数学</ulink> 関数のマニュアルを参照してください。
|
|
</para></listitem>
|
|
|
|
<listitem><para>
|
|
<parameter>assign</parameter> 属性を指定すると、
|
|
<varname>{math}</varname> 関数の出力はテンプレート変数に格納され、
|
|
テンプレートには出力されません。
|
|
</para></listitem>
|
|
</itemizedlist>
|
|
|
|
<note>
|
|
<title>テクニカルノート</title>
|
|
<para>
|
|
<varname>{math}</varname> は PHP の
|
|
<ulink url="&url.php-manual;eval"><varname>eval()</varname></ulink>
|
|
関数を使用するのでパフォーマンス的にコストの高い関数です。
|
|
PHP 内で math 関数を実行する事は、テンプレートで行うよりもはるかに効率的で、
|
|
mathの計算がPHPで可能な場合はPHPで行い、結果をテンプレートに
|
|
<link linkend="api.assign"><varname>assign()</varname></link> するようにしましょう。
|
|
<link linkend="language.function.section">
|
|
<varname>{section}</varname></link> ループ内のような反復動作で
|
|
<varname>{math}</varname> 関数を呼び出す事は避けて下さい。
|
|
</para>
|
|
</note>
|
|
|
|
<informaltable frame="all">
|
|
<tgroup cols="5">
|
|
<colspec colname="param" align="center" />
|
|
<colspec colname="type" align="center" />
|
|
<colspec colname="required" align="center" />
|
|
<colspec colname="default" align="center" />
|
|
<colspec colname="desc" />
|
|
<thead>
|
|
<row>
|
|
<entry>属性名</entry>
|
|
<entry>型</entry>
|
|
<entry>必須</entry>
|
|
<entry>デフォルト</entry>
|
|
<entry>概要</entry>
|
|
</row>
|
|
</thead>
|
|
<tbody>
|
|
<row>
|
|
<entry>equation</entry>
|
|
<entry>string</entry>
|
|
<entry>Yes</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>実行する式</entry>
|
|
</row>
|
|
<row>
|
|
<entry>format</entry>
|
|
<entry>string</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>結果の表示フォーマット (sprintf)</entry>
|
|
</row>
|
|
<row>
|
|
<entry>var</entry>
|
|
<entry>numeric</entry>
|
|
<entry>Yes</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>式の変数に渡す値</entry>
|
|
</row>
|
|
<row>
|
|
<entry>assign</entry>
|
|
<entry>string</entry>
|
|
<entry>No</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>出力を割り当てるテンプレート変数</entry>
|
|
</row>
|
|
<row>
|
|
<entry>[var ...]</entry>
|
|
<entry>numeric</entry>
|
|
<entry>Yes</entry>
|
|
<entry><emphasis>n/a</emphasis></entry>
|
|
<entry>式の変数の値</entry>
|
|
</row>
|
|
</tbody>
|
|
</tgroup>
|
|
</informaltable>
|
|
|
|
|
|
<example>
|
|
<title>{math}</title>
|
|
<para>
|
|
<emphasis role="bold">サンプル a:</emphasis>
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* $height=4, $width=5 *}
|
|
|
|
{math equation="x + y" x=$height y=$width}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
9
|
|
]]>
|
|
</screen>
|
|
<para>
|
|
<emphasis role="bold">サンプル b:</emphasis>
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* $row_height = 10, $row_width = 20, #col_div# = 2, テンプレートで割り当てます *}
|
|
|
|
{math equation="height * width / division"
|
|
height=$row_height
|
|
width=$row_width
|
|
division=#col_div#}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
100
|
|
]]>
|
|
</screen>
|
|
<para>
|
|
<emphasis role="bold">サンプル c:</emphasis>
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* 括弧も使用できます *}
|
|
|
|
{math equation="(( x + y ) / z )" x=2 y=10 z=2}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
6
|
|
]]>
|
|
</screen>
|
|
<para>
|
|
<emphasis role="bold">サンプル d:</emphasis>
|
|
</para>
|
|
<programlisting>
|
|
<![CDATA[
|
|
{* sprintf 形式のフォーマット文字列を指定できます *}
|
|
|
|
{math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}
|
|
]]>
|
|
</programlisting>
|
|
<para>
|
|
上の例の出力
|
|
</para>
|
|
<screen>
|
|
<![CDATA[
|
|
9.44
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</sect1>
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"../../../../manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|