mirror of
				https://github.com/smarty-php/smarty.git
				synced 2025-11-03 22:01:36 +01:00 
			
		
		
		
	
		
			
	
	
		
			146 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
		
		
			
		
	
	
			146 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			XML
		
	
	
	
	
	
| 
								 | 
							
								<?xml version="1.0" encoding="iso-8859-1"?>
							 | 
						||
| 
								 | 
							
								<!-- $Revision$ -->
							 | 
						||
| 
								 | 
							
										<sect1 id="language.function.math">
							 | 
						||
| 
								 | 
							
											<title>math</title>
							 | 
						||
| 
								 | 
							
								            <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>
							 | 
						||
| 
								 | 
							
											<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>
							 | 
						||
| 
								 | 
							
											<note>
							 | 
						||
| 
								 | 
							
												<title>Technical Note</title>
							 | 
						||
| 
								 | 
							
												<para>
							 | 
						||
| 
								 | 
							
												math is an expensive function in performance due to its use of
							 | 
						||
| 
								 | 
							
												the php eval() function. Doing the math in PHP is much more
							 | 
						||
| 
								 | 
							
												efficient, so whenever possible do the math calculations in PHP
							 | 
						||
| 
								 | 
							
												and assign the results to the template. Definately avoid
							 | 
						||
| 
								 | 
							
												repetitive math function calls, like within section loops.
							 | 
						||
| 
								 | 
							
								            	</para>
							 | 
						||
| 
								 | 
							
											</note>
							 | 
						||
| 
								 | 
							
								<example>
							 | 
						||
| 
								 | 
							
								<title>math</title>
							 | 
						||
| 
								 | 
							
								<programlisting>
							 | 
						||
| 
								 | 
							
								{* $height=4, $width=5 *}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								{math equation="x + y" x=$height y=$width}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								OUTPUT:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								9
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								{* $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#}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								OUTPUT:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								100
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								{* you can use parenthesis *}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								{math equation="(( x + y ) / z )" x=2 y=10 z=2}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								OUTPUT:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								6
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								{* you can supply a format parameter in sprintf format *}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								{math equation="x + y" x=4.4444 y=5.0000 format="%.2f"}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								OUTPUT:
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								9.44</programlisting>
							 | 
						||
| 
								 | 
							
								</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
							 | 
						||
| 
								 | 
							
								-->
							 |