| 
									
										
										
										
											2004-04-13 08:46:28 +00:00
										 |  |  | <?xml version="1.0" encoding="iso-8859-1"?> | 
					
						
							|  |  |  | <!-- $Revision$ --> | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  | <sect1 id="language.function.math"> | 
					
						
							| 
									
										
										
										
											2005-05-25 19:13:20 +00:00
										 |  |  |  <title>{math}</title> | 
					
						
							| 
									
										
										
										
											2005-09-13 19:03:11 +00:00
										 |  |  |   <para> | 
					
						
							|  |  |  |    {math} allows the template designer to do math equations in the | 
					
						
							|  |  |  |    template. Any numeric template variables may be used in the | 
					
						
							|  |  |  |    equations, and the result is printed in place of the tag. The | 
					
						
							|  |  |  |    variables used in the equation are passed as parameters, which can | 
					
						
							|  |  |  |    be template variables or static values. +, -, /, *, abs, ceil, cos, | 
					
						
							|  |  |  |    exp, floor, log, log10, max, min, pi, pow, rand, round, sin, sqrt, | 
					
						
							|  |  |  |    srans and tan are all valid operators. Check the PHP documentation | 
					
						
							|  |  |  |    for further information on these math functions. | 
					
						
							|  |  |  |   </para> | 
					
						
							|  |  |  |   <para> | 
					
						
							|  |  |  |    If you supply the special "assign" attribute, the output of the | 
					
						
							|  |  |  |    math function will be assigned to this template variable instead of | 
					
						
							|  |  |  |    being output to the template. | 
					
						
							|  |  |  |  </para> | 
					
						
							|  |  |  |   | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |  <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>Attribute Name</entry> | 
					
						
							|  |  |  |      <entry>Type</entry> | 
					
						
							|  |  |  |      <entry>Required</entry> | 
					
						
							|  |  |  |      <entry>Default</entry> | 
					
						
							|  |  |  |      <entry>Description</entry> | 
					
						
							|  |  |  |     </row> | 
					
						
							|  |  |  |    </thead> | 
					
						
							|  |  |  |    <tbody> | 
					
						
							|  |  |  |     <row> | 
					
						
							|  |  |  |      <entry>equation</entry> | 
					
						
							|  |  |  |      <entry>string</entry> | 
					
						
							|  |  |  |      <entry>Yes</entry> | 
					
						
							|  |  |  |      <entry><emphasis>n/a</emphasis></entry> | 
					
						
							|  |  |  |      <entry>the equation to execute</entry> | 
					
						
							|  |  |  |     </row> | 
					
						
							|  |  |  |     <row> | 
					
						
							|  |  |  |      <entry>format</entry> | 
					
						
							|  |  |  |      <entry>string</entry> | 
					
						
							|  |  |  |      <entry>No</entry> | 
					
						
							|  |  |  |      <entry><emphasis>n/a</emphasis></entry> | 
					
						
							|  |  |  |      <entry>the format of the result (sprintf)</entry> | 
					
						
							|  |  |  |     </row> | 
					
						
							|  |  |  |     <row> | 
					
						
							|  |  |  |      <entry>var</entry> | 
					
						
							|  |  |  |      <entry>numeric</entry> | 
					
						
							|  |  |  |      <entry>Yes</entry> | 
					
						
							|  |  |  |      <entry><emphasis>n/a</emphasis></entry> | 
					
						
							|  |  |  |      <entry>equation variable value</entry> | 
					
						
							|  |  |  |     </row> | 
					
						
							|  |  |  |     <row> | 
					
						
							|  |  |  |      <entry>assign</entry> | 
					
						
							|  |  |  |      <entry>string</entry> | 
					
						
							|  |  |  |      <entry>No</entry> | 
					
						
							|  |  |  |      <entry><emphasis>n/a</emphasis></entry> | 
					
						
							|  |  |  |      <entry>template variable the output will be assigned to</entry> | 
					
						
							|  |  |  |     </row> | 
					
						
							|  |  |  |     <row> | 
					
						
							|  |  |  |      <entry>[var ...]</entry> | 
					
						
							|  |  |  |      <entry>numeric</entry> | 
					
						
							|  |  |  |      <entry>Yes</entry> | 
					
						
							|  |  |  |      <entry><emphasis>n/a</emphasis></entry> | 
					
						
							|  |  |  |      <entry>equation variable value</entry> | 
					
						
							|  |  |  |     </row> | 
					
						
							|  |  |  |    </tbody> | 
					
						
							|  |  |  |   </tgroup> | 
					
						
							|  |  |  |  </informaltable> | 
					
						
							| 
									
										
										
										
											2005-09-13 19:03:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |  <note> | 
					
						
							|  |  |  |   <title>Technical Note</title> | 
					
						
							|  |  |  |   <para> | 
					
						
							| 
									
										
										
										
											2005-05-25 19:13:20 +00:00
										 |  |  |    {math} is an expensive function in performance due to its use of the php | 
					
						
							|  |  |  |    <ulink url="&url.php-manual;eval">eval()</ulink> function. Doing the math in PHP is much | 
					
						
							|  |  |  |    more | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |    efficient, so whenever possible do the math calculations in PHP | 
					
						
							| 
									
										
										
										
											2005-05-25 19:13:20 +00:00
										 |  |  |    and <link linkend="api.assign">assign()</link> | 
					
						
							|  |  |  |    the results to the template. Definately avoid | 
					
						
							|  |  |  |    repetitive {math} function calls, eg within | 
					
						
							|  |  |  |    <link linkend="language.function.section">{section}</link> loops. | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |   </para> | 
					
						
							|  |  |  |  </note> | 
					
						
							|  |  |  |  <example> | 
					
						
							| 
									
										
										
										
											2005-05-25 19:13:20 +00:00
										 |  |  |   <title>{math}</title> | 
					
						
							|  |  |  |   <para> | 
					
						
							|  |  |  |   <emphasis role="bold">Example a:</emphasis> | 
					
						
							|  |  |  |   </para> | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |   <programlisting> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    {* $height=4, $width=5 *} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    {math equation="x + y" x=$height y=$width} | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  |   </programlisting> | 
					
						
							|  |  |  |   <para> | 
					
						
							|  |  |  |    The above example will output: | 
					
						
							|  |  |  |   </para> | 
					
						
							|  |  |  |   <screen> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    9 | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  |   </screen> | 
					
						
							| 
									
										
										
										
											2005-05-25 19:13:20 +00:00
										 |  |  |   <para> | 
					
						
							|  |  |  |     <emphasis role="bold">Example b:</emphasis> | 
					
						
							|  |  |  |   </para> | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |   <programlisting> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    {* $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#} | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  |   </programlisting> | 
					
						
							|  |  |  |   <para> | 
					
						
							|  |  |  |    The above example will output: | 
					
						
							|  |  |  |   </para> | 
					
						
							|  |  |  |   <screen> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    100 | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  |   </screen> | 
					
						
							| 
									
										
										
										
											2005-05-25 19:13:20 +00:00
										 |  |  |   <para> | 
					
						
							|  |  |  |   <emphasis role="bold">Example c:</emphasis> | 
					
						
							|  |  |  |   </para> | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |   <programlisting> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    {* you can use parenthesis *} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    {math equation="(( x + y ) / z )" x=2 y=10 z=2} | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  |   </programlisting> | 
					
						
							|  |  |  |   <para> | 
					
						
							|  |  |  |    The above example will output: | 
					
						
							|  |  |  |   </para> | 
					
						
							|  |  |  |   <screen> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    6 | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  |   </screen> | 
					
						
							| 
									
										
										
										
											2005-05-25 19:13:20 +00:00
										 |  |  |     <para> | 
					
						
							|  |  |  |     <emphasis role="bold">Example d:</emphasis> | 
					
						
							|  |  |  |   </para> | 
					
						
							| 
									
										
										
										
											2005-05-10 21:14:36 +00:00
										 |  |  |   <programlisting> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    {* you can supply a format parameter in sprintf format *} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    {math equation="x + y" x=4.4444 y=5.0000 format="%.2f"} | 
					
						
							|  |  |  |    ]]> | 
					
						
							|  |  |  |   </programlisting> | 
					
						
							|  |  |  |   <para> | 
					
						
							|  |  |  |    The above example will output: | 
					
						
							|  |  |  |   </para> | 
					
						
							|  |  |  |   <screen> | 
					
						
							|  |  |  | <![CDATA[ | 
					
						
							|  |  |  |    9.44 | 
					
						
							|  |  |  | ]]> | 
					
						
							|  |  |  |   </screen> | 
					
						
							|  |  |  |  </example> | 
					
						
							| 
									
										
										
										
											2004-04-13 08:46:28 +00:00
										 |  |  | </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 | 
					
						
							|  |  |  | --> |