<HTML ><HEAD ><TITLE >{section},{sectionelse}</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Smarty Manual" HREF="index.html"><LINK REL="UP" TITLE="Built-in Functions" HREF="language.builtin.functions.html"><LINK REL="PREVIOUS" TITLE="{php}" HREF="language.function.php.html"><LINK REL="NEXT" TITLE="{strip}" HREF="language.function.strip.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Smarty Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.function.php.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 7. Built-in Functions</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.function.strip.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.function.section" ></A >{section},{sectionelse}</H1 ><P > A <TT CLASS="varname" >{section}</TT > is for looping over <SPAN CLASS="bold" ><B CLASS="emphasis" >arrays of data</B ></SPAN >, unlike <A HREF="language.function.foreach.html" ><TT CLASS="varname" >{foreach}</TT ></A > which is used to loop over a <SPAN CLASS="bold" ><B CLASS="emphasis" >single associative array</B ></SPAN >. Every <TT CLASS="varname" >{section}</TT > tag must be paired with a closing <TT CLASS="varname" >{/section}</TT > tag. </P ><DIV CLASS="informaltable" ><A NAME="AEN2324" ></A ><P ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><THEAD ><TR ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Attribute Name</TH ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Type</TH ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Required</TH ><TH WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Default</TH ><TH WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >name</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >string</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Yes</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" >n/a</I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >The name of the section</TD ></TR ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >loop</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >mixed</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >Yes</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" >n/a</I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >Value to determine the number of loop iterations</TD ></TR ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >start</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >integer</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >No</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" >0</I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >The index position that the section will begin looping. If the value is negative, the start position is calculated from the end of the array. For example, if there are seven values in the loop array and start is -2, the start index is 5. Invalid values (values outside of the length of the loop array) are automatically truncated to the closest valid value.</TD ></TR ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >step</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >integer</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >No</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" >1</I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >The step value that will be used to traverse the loop array. For example, step=2 will loop on index 0,2,4, etc. If step is negative, it will step through the array backwards.</TD ></TR ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >max</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >integer</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >No</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" >n/a</I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >Sets the maximum number of times the section will loop.</TD ></TR ><TR ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >show</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >boolean</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" >No</TD ><TD WIDTH="20%" ALIGN="CENTER" VALIGN="MIDDLE" ><SPAN CLASS="emphasis" ><I CLASS="emphasis" ><TT CLASS="constant" >TRUE</TT ></I ></SPAN ></TD ><TD WIDTH="20%" ALIGN="LEFT" VALIGN="MIDDLE" >determines whether or not to show this section</TD ></TR ></TBODY ></TABLE ><P ></P ></DIV ><P ></P ><UL ><LI ><P > Required attributes are <TT CLASS="parameter" ><I >name</I ></TT > and <TT CLASS="parameter" ><I >loop</I ></TT >. </P ></LI ><LI ><P > The <TT CLASS="parameter" ><I >name</I ></TT > of the <TT CLASS="varname" >{section}</TT > can be anything you like, made up of letters, numbers and underscores, like <A HREF="http://php.net/language.variables" TARGET="_top" >PHP variables</A >. </P ></LI ><LI ><P > Sections can be nested, and the nested <TT CLASS="varname" >{section}</TT > names must be unique from each other. </P ></LI ><LI ><P > The <TT CLASS="parameter" ><I >loop</I ></TT > attribute, usually an array of values, determines the number of times the <TT CLASS="varname" >{section}</TT > will loop. </P ></LI ><LI ><P >When printing a variable within a <TT CLASS="varname" >{section}</TT >, the <TT CLASS="varname" >{section}</TT > <TT CLASS="parameter" ><I >name</I ></TT > must be given next to variable name within [brackets]. </P ></LI ><LI ><P > <TT CLASS="varname" >{sectionelse}</TT > is executed when there are no values in the loop variable. </P ></LI ><LI ><P > A <TT CLASS="varname" >{section}</TT > also has its own variables that handle <TT CLASS="varname" >{section}</TT > properties. These properties are accessible as: <A HREF="language.variables.smarty.html#language.variables.smarty.loops" > <TT CLASS="parameter" ><I >{$smarty.section.name.property}</I ></TT ></A > where <SPAN CLASS="QUOTE" >"name"</SPAN > is the attribute <TT CLASS="parameter" ><I >name</I ></TT >. </P ></LI ><LI ><P > <TT CLASS="varname" >{section}</TT > properties are <A HREF="language.function.section.html#section.property.index" ><TT CLASS="parameter" ><I >index</I ></TT ></A >, <A HREF="language.function.section.html#section.property.index.prev" ><TT CLASS="parameter" ><I >index_prev</I ></TT ></A >, <A HREF="language.function.section.html#section.property.index.next" ><TT CLASS="parameter" ><I >index_next</I ></TT ></A >, <A HREF="language.function.section.html#section.property.iteration" ><TT CLASS="parameter" ><I >iteration</I ></TT ></A >, <A HREF="language.function.section.html#section.property.first" ><TT CLASS="parameter" ><I >first</I ></TT ></A >, <A HREF="language.function.section.html#section.property.last" ><TT CLASS="parameter" ><I >last</I ></TT ></A >, <A HREF="language.function.section.html#section.property.rownum" ><TT CLASS="parameter" ><I >rownum</I ></TT ></A >, <A HREF="language.function.section.html#section.property.loop" ><TT CLASS="parameter" ><I >loop</I ></TT ></A >, <A HREF="language.function.section.html#section.property.show" ><TT CLASS="parameter" ><I >show</I ></TT ></A >, <A HREF="language.function.section.html#section.property.total" ><TT CLASS="parameter" ><I >total</I ></TT ></A >. </P ></LI ></UL ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2438" ></A ><P ><B >Example 7-30. Looping a simple array with {section}</B ></P ><P ><A HREF="api.assign.html" >assign()</A > an array to Smarty from the php script</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$data </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1000</span><span style="color: #007700">,</span><span style="color: #0000BB">1001</span><span style="color: #007700">,</span><span style="color: #0000BB">1002</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'custid'</span><span style="color: #007700">,</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P >The template that outputs the array</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{* this example will print out all the values of the $custid array *} {section name=customer loop=$custid} id: {$custid[customer]}<br /> {/section} <hr /> {* print out all the values of the $custid array reversed *} {section name=foo loop=$custid step=-1} {$custid[foo]}<br /> {/section}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >id: 1000<br /> id: 1001<br /> id: 1002<br /> <hr /> id: 1002<br /> id: 1001<br /> id: 1000<br /></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2447" ></A ><P ><B >Example 7-31. {section} without an assigned array</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=foo start=10 loop=20 step=2} {$smarty.section.foo.index} {/section} <hr /> {section name=bar loop=21 max=6 step=-2} {$smarty.section.bar.index} {/section}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >10 12 14 16 18 <hr /> 20 18 16 14 12 10</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2452" ></A ><P ><B >Example 7-32. Naming a {section}</B ></P ><P >The <TT CLASS="parameter" ><I >name</I ></TT > of the <TT CLASS="varname" >{section}</TT > can be anything you like, see <A HREF="http://php.net/language.variables" TARGET="_top" >PHP variables</A >. It is used to reference the data within the <TT CLASS="varname" >{section}</TT >.</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=anything loop=$myArray} {$myArray[anything].foo} {$name[anything]} {$address[anything].bar} {/section}</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2460" ></A ><P ><B >Example 7-33. Looping an associative array's with {section}</B ></P ><P >This is an example of printing an associative array of data within a <TT CLASS="varname" >{section}</TT >. Following is the php script to assign the <TT CLASS="parameter" ><I >$contacts</I ></TT > array to Smarty.</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$data </span><span style="color: #007700">= array(<br /> array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'John Smith'</span><span style="color: #007700">, </span><span style="color: #DD0000">'home' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'555-555-5555'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'cell' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'666-555-5555'</span><span style="color: #007700">, </span><span style="color: #DD0000">'email' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'john@myexample.com'</span><span style="color: #007700">),<br /> array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Jack Jones'</span><span style="color: #007700">, </span><span style="color: #DD0000">'home' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'777-555-5555'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'cell' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'888-555-5555'</span><span style="color: #007700">, </span><span style="color: #DD0000">'email' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'jack@myexample.com'</span><span style="color: #007700">),<br /> array(</span><span style="color: #DD0000">'name' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'Jane Munson'</span><span style="color: #007700">, </span><span style="color: #DD0000">'home' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'000-555-5555'</span><span style="color: #007700">,<br /> </span><span style="color: #DD0000">'cell' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'123456'</span><span style="color: #007700">, </span><span style="color: #DD0000">'email' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'jane@myexample.com'</span><span style="color: #007700">)<br /> );<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'contacts'</span><span style="color: #007700">,</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P >The template to output <TT CLASS="parameter" ><I >$contacts</I ></TT ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$contacts} <p> name: {$contacts[customer].name}<br /> home: {$contacts[customer].home}<br /> cell: {$contacts[customer].cell}<br /> e-mail: {$contacts[customer].email} </p> {/section}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" ><p> name: John Smith<br /> home: 555-555-5555<br /> cell: 666-555-5555<br /> e-mail: john@myexample.com </p> <p> name: Jack Jones<br /> home phone: 777-555-5555<br /> cell phone: 888-555-5555<br /> e-mail: jack@myexample.com </p> <p> name: Jane Munson<br /> home phone: 000-555-5555<br /> cell phone: 123456<br /> e-mail: jane@myexample.com </p></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2471" ></A ><P ><B >Example 7-34. {section} demonstrating the <TT CLASS="varname" >loop</TT > variable</B ></P ><P >This example assumes that <TT CLASS="parameter" ><I >$custid</I ></TT >, <TT CLASS="parameter" ><I >$name</I ></TT > and <TT CLASS="parameter" ><I >$address</I ></TT > are all arrays containing the same number of values. First the php script that assign's the arrays to Smarty.</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$id </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1001</span><span style="color: #007700">,</span><span style="color: #0000BB">1002</span><span style="color: #007700">,</span><span style="color: #0000BB">1003</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'custid'</span><span style="color: #007700">,</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fullnames </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'John Smith'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Jack Jones'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Jane Munson'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'name'</span><span style="color: #007700">,</span><span style="color: #0000BB">$fullnames</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$addr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'253 Abbey road'</span><span style="color: #007700">, </span><span style="color: #DD0000">'417 Mulberry ln'</span><span style="color: #007700">, </span><span style="color: #DD0000">'5605 apple st'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'address'</span><span style="color: #007700">,</span><span style="color: #0000BB">$addr</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P >The <TT CLASS="parameter" ><I >loop</I ></TT > variable only determines the number of times to loop. You can access ANY variable from the template within the <TT CLASS="varname" >{section}</TT ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$custid} <p> id: {$custid[customer]}<br /> name: {$name[customer]}<br /> address: {$address[customer]} </p> {/section}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" ><p> id: 1000<br /> name: John Smith<br /> address: 253 Abbey road </p> <p> id: 1001<br /> name: Jack Jones<br /> address: 417 Mulberry ln </p> <p> id: 1002<br /> name: Jane Munson<br /> address: 5605 apple st </p></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2485" ></A ><P ><B >Example 7-35. Nested {section}'s</B ></P ><P > Sections can be nested as deep as you like. With nested sections, you can access complex data structures, such as multi-dimensional arrays. This is a php script thats assign's the arrays. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$id </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1001</span><span style="color: #007700">,</span><span style="color: #0000BB">1002</span><span style="color: #007700">,</span><span style="color: #0000BB">1003</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'custid'</span><span style="color: #007700">,</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$fullnames </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'John Smith'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Jack Jones'</span><span style="color: #007700">,</span><span style="color: #DD0000">'Jane Munson'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'name'</span><span style="color: #007700">,</span><span style="color: #0000BB">$fullnames</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$addr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'253 N 45th'</span><span style="color: #007700">, </span><span style="color: #DD0000">'417 Mulberry ln'</span><span style="color: #007700">, </span><span style="color: #DD0000">'5605 apple st'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'address'</span><span style="color: #007700">,</span><span style="color: #0000BB">$addr</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$types </span><span style="color: #007700">= array(<br /> array( </span><span style="color: #DD0000">'home phone'</span><span style="color: #007700">, </span><span style="color: #DD0000">'cell phone'</span><span style="color: #007700">, </span><span style="color: #DD0000">'e-mail'</span><span style="color: #007700">),<br /> array( </span><span style="color: #DD0000">'home phone'</span><span style="color: #007700">, </span><span style="color: #DD0000">'web'</span><span style="color: #007700">),<br /> array( </span><span style="color: #DD0000">'cell phone'</span><span style="color: #007700">)<br /> );<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'contact_type'</span><span style="color: #007700">, </span><span style="color: #0000BB">$types</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$info </span><span style="color: #007700">= array(<br /> array(</span><span style="color: #DD0000">'555-555-5555'</span><span style="color: #007700">, </span><span style="color: #DD0000">'666-555-5555'</span><span style="color: #007700">, </span><span style="color: #DD0000">'john@myexample.com'</span><span style="color: #007700">),<br /> array( </span><span style="color: #DD0000">'123-456-4'</span><span style="color: #007700">, </span><span style="color: #DD0000">'www.example.com'</span><span style="color: #007700">),<br /> array( </span><span style="color: #DD0000">'0457878'</span><span style="color: #007700">)<br /> );<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'contact_info'</span><span style="color: #007700">, </span><span style="color: #0000BB">$info</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P >In this template, <SPAN CLASS="emphasis" ><I CLASS="emphasis" >$contact_type[customer]</I ></SPAN > is an array of contact types for the current customer.</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$custid} <hr> id: {$custid[customer]}<br /> name: {$name[customer]}<br /> address: {$address[customer]}<br /> {section name=contact loop=$contact_type[customer]} {$contact_type[customer][contact]}: {$contact_info[customer][contact]}<br /> {/section} {/section}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" ><hr> id: 1000<br /> name: John Smith<br /> address: 253 N 45th<br /> home phone: 555-555-5555<br /> cell phone: 666-555-5555<br /> e-mail: john@myexample.com<br /> <hr> id: 1001<br /> name: Jack Jones<br /> address: 417 Mulberry ln<br /> home phone: 123-456-4<br /> web: www.example.com<br /> <hr> id: 1002<br /> name: Jane Munson<br /> address: 5605 apple st<br /> cell phone: 0457878<br /></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2494" ></A ><P ><B >Example 7-36. Database example with a {sectionelse}</B ></P ><P >Results of a database search (eg ADODB or PEAR) are assigned to Smarty</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'select id, name, home, cell, email from contacts '<br /> </span><span style="color: #007700">.</span><span style="color: #DD0000">"where name like '$foo%' "</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'contacts'</span><span style="color: #007700">, </span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">getAll</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P >The template to output the database result in a HTML table</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><table> <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr> {section name=co loop=$contacts} <tr> <td><a href="view.php?id={$contacts[co].id}">view<a></td> <td>{$contacts[co].name}</td> <td>{$contacts[co].home}</td> <td>{$contacts[co].cell}</td> <td>{$contacts[co].email}</td> <tr> {sectionelse} <tr><td colspan="5">No items found</td></tr> {/section} </table></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.index" ></A >.index</H2 ><P > <TT CLASS="parameter" ><I >index</I ></TT > contains the current array index, starting with zero or the <TT CLASS="parameter" ><I >start</I ></TT > attribute if given It increments by one or by the <TT CLASS="parameter" ><I >step</I ></TT > attribute if given. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Technical Note: </B > If the <TT CLASS="parameter" ><I >step</I ></TT > and <TT CLASS="parameter" ><I >start</I ></TT > properties are not modified, then this works the same as the <A HREF="language.function.section.html#section.property.iteration" ><TT CLASS="parameter" ><I >iteration</I ></TT ></A > property, except it starts at zero instead of one. </P ></BLOCKQUOTE ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2513" ></A ><P ><B >Example 7-37. {section} <TT CLASS="varname" >index</TT > property</B ></P ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >FYI: </B ><TT CLASS="parameter" ><I >$custid[customer.index]</I ></TT > and <TT CLASS="varname" >$custid[customer]</TT > are identical in meaning.</P ></BLOCKQUOTE ></DIV > </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >0 id: 1000<br /> 1 id: 1001<br /> 2 id: 1002<br /></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.index.prev" ></A >.index_prev</H2 ><P > <TT CLASS="parameter" ><I >index_prev</I ></TT > is the previous loop index. On the first loop, this is set to -1. </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.index.next" ></A >.index_next</H2 ><P > <TT CLASS="parameter" ><I >index_next</I ></TT > is the next loop index. On the last loop, this is still one more than the current index, respecting the setting of the <TT CLASS="parameter" ><I >step</I ></TT > attribute, if given. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2534" ></A ><P ><B >Example 7-38. <TT CLASS="varname" >index</TT >, <TT CLASS="varname" >index_next</TT > and <TT CLASS="varname" >index_prev</TT > properties </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$data </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1001</span><span style="color: #007700">,</span><span style="color: #0000BB">1002</span><span style="color: #007700">,</span><span style="color: #0000BB">1003</span><span style="color: #007700">,</span><span style="color: #0000BB">1004</span><span style="color: #007700">,</span><span style="color: #0000BB">1005</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'rows'</span><span style="color: #007700">,</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P >Template to output the above array in a table</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{* $rows[row.index] and $rows[row] are identical in meaning *} <table> <tr> <th>index</th><th>id</th> <th>index_prev</th><th>prev_id</th> <th>index_next</th><th>next_id</th> </tr> {section name=row loop=$rows} <tr> <td>{$smarty.section.row.index}</td><td>{$rows[row]}</td> <td>{$smarty.section.row.index_prev}</td><td>{$rows[row.index_prev]}</td> <td>{$smarty.section.row.index_next}</td><td>{$rows[row.index_next]}</td> </tr> {/section} </table></PRE ></TD ></TR ></TABLE ><P > The above example will output a table containing the following: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >index id index_prev prev_id index_next next_id 0 1001 -1 1 1002 1 1002 0 1001 2 1003 2 1003 1 1002 3 1004 3 1004 2 1003 4 1005 4 1005 3 1004 5</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.iteration" ></A >.iteration</H2 ><P > <TT CLASS="parameter" ><I >iteration</I ></TT > contains the current loop iteration and starts at one. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > This is not affected by the <TT CLASS="varname" >{section}</TT > properties <TT CLASS="parameter" ><I >start</I ></TT >, <TT CLASS="parameter" ><I >step</I ></TT > and <TT CLASS="parameter" ><I >max</I ></TT >, unlike the <A HREF="language.function.section.html#section.property.index" ><TT CLASS="parameter" ><I >index</I ></TT ></A > property. <TT CLASS="parameter" ><I >iteration</I ></TT > also starts with one instead of zero unlike <TT CLASS="parameter" ><I >index</I ></TT >. <A HREF="language.function.section.html#section.property.rownum" ><TT CLASS="parameter" ><I >rownum</I ></TT ></A > is an alias to <TT CLASS="parameter" ><I >iteration</I ></TT >, they are identical. </P ></BLOCKQUOTE ></DIV ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2561" ></A ><P ><B >Example 7-39. A section's <TT CLASS="varname" >iteration</TT > property </B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// array of 3000 to 3015<br /></span><span style="color: #0000BB">$id </span><span style="color: #007700">= </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">3000</span><span style="color: #007700">,</span><span style="color: #0000BB">3015</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$smarty</span><span style="color: #007700">-></span><span style="color: #0000BB">assign</span><span style="color: #007700">(</span><span style="color: #DD0000">'arr'</span><span style="color: #007700">,</span><span style="color: #0000BB">$id</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></TD ></TR ></TABLE ><P >Template to output every other element of the <TT CLASS="varname" >$arr</TT > array as <TT CLASS="varname" >step</TT >=2</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=cu loop=$arr start=5 step=2} iteration={$smarty.section.cu.iteration} index={$smarty.section.cu.index} id={$custid[cu]}<br /> {/section}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >iteration=1 index=5 id=3005<br /> iteration=2 index=7 id=3007<br /> iteration=3 index=9 id=3009<br /> iteration=4 index=11 id=3011<br /> iteration=5 index=13 id=3013<br /> iteration=6 index=15 id=3015<br /></PRE ></TD ></TR ></TABLE ><P > Another example uses the <TT CLASS="parameter" ><I >iteration</I ></TT > property to output a table header block every five rows and uses <A HREF="language.function.if.html" ><TT CLASS="varname" >{if}</TT ></A > with the mod operator. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" ><table> {section name=co loop=$contacts} {if $smarty.section.co.iteration % 5 == 1} <tr><th>&nbsp;</th><th>Name></th><th>Home</th><th>Cell</th><th>Email</th></tr> {/if} <tr> <td><a href="view.php?id={$contacts[co].id}">view<a></td> <td>{$contacts[co].name}</td> <td>{$contacts[co].home}</td> <td>{$contacts[co].cell}</td> <td>{$contacts[co].email}</td> <tr> {/section} </table></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.first" ></A >.first</H2 ><P > <TT CLASS="parameter" ><I >first</I ></TT > is set to <TT CLASS="constant" >TRUE</TT > if the current <TT CLASS="varname" >{section}</TT > iteration is the initial one. </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.last" ></A >.last</H2 ><P > <TT CLASS="parameter" ><I >last</I ></TT > is set to <TT CLASS="constant" >TRUE</TT > if the current section iteration is the final one. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2587" ></A ><P ><B >Example 7-40. {section} property <TT CLASS="varname" >first</TT > and <TT CLASS="varname" >last</TT ></B ></P ><P > This example loops the <TT CLASS="varname" >$customers</TT > array, outputs a header block on the first iteration and on the last outputs the footer block. Also uses the <A HREF="language.function.section.html#section.property.total" ><TT CLASS="parameter" ><I >total</I ></TT ></A > property. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$customers} {if $smarty.section.customer.first} <table> <tr><th>id</th><th>customer</th></tr> {/if} <tr> <td>{$customers[customer].id}}</td> <td>{$customers[customer].name}</td> </tr> {if $smarty.section.customer.last} <tr><td></td><td>{$smarty.section.customer.total} customers</td></tr> </table> {/if} {/section}</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.rownum" ></A >.rownum</H2 ><P > <TT CLASS="parameter" ><I >rownum</I ></TT > contains the current loop iteration, starting with one. It is an alias to <A HREF="language.function.section.html#section.property.iteration" ><TT CLASS="parameter" ><I >iteration</I ></TT ></A >, they work identically. </P ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.loop" ></A >.loop</H2 ><P > <TT CLASS="parameter" ><I >loop</I ></TT > contains the last index number that this {section} looped. This can be used inside or after the <TT CLASS="varname" >{section}</TT >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2607" ></A ><P ><B >Example 7-41. {section} property <TT CLASS="varname" >loop</TT >s</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$custid} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section} There are {$smarty.section.customer.loop} customers shown above.</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >0 id: 1000<br /> 1 id: 1001<br /> 2 id: 1002<br /> There are 3 customers shown above.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.show" ></A >.show</H2 ><P > <TT CLASS="parameter" ><I >show</I ></TT > is used as a parameter to section. <TT CLASS="parameter" ><I >show</I ></TT > is a boolean value, <TT CLASS="constant" >TRUE</TT > or <TT CLASS="constant" >FALSE</TT >. If <TT CLASS="constant" >FALSE</TT >, the section will not be displayed. If there is a <TT CLASS="varname" >{sectionelse}</TT > present, that will be alternately displayed. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2622" ></A ><P ><B >Example 7-42. <TT CLASS="varname" >show</TT > property </B ></P ><P >Boolean <TT CLASS="varname" >$show_customer_info</TT > has been passed from the PHP application, to regulate whether or not this section shows.</P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$customers show=$show_customer_info} {$smarty.section.customer.rownum} id: {$customers[customer]}<br /> {/section} {if $smarty.section.customer.show} the section was shown. {else} the section was not shown. {/if}</PRE ></TD ></TR ></TABLE ><P > The above example will output: </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="screen" >1 id: 1000<br /> 2 id: 1001<br /> 3 id: 1002<br /> the section was shown.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="sect2" ><H2 CLASS="sect2" ><A NAME="section.property.total" ></A >.total</H2 ><P > <TT CLASS="parameter" ><I >total</I ></TT > contains the number of iterations that this <TT CLASS="varname" >{section}</TT > will loop. This can be used inside or after a <TT CLASS="varname" >{section}</TT >. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN2636" ></A ><P ><B >Example 7-43. <TT CLASS="varname" >total</TT > property example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >{section name=customer loop=$custid step=2} {$smarty.section.customer.index} id: {$custid[customer]}<br /> {/section} There are {$smarty.section.customer.total} customers shown above.</PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><P > See also <A HREF="language.function.foreach.html" ><TT CLASS="varname" >{foreach}</TT ></A > and <A HREF="language.variables.smarty.html#language.variables.smarty.loops" ><TT CLASS="parameter" ><I >$smarty.section</I ></TT ></A >. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.function.php.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.function.strip.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >{php}</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.builtin.functions.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >{strip}</TD ></TR ></TABLE ></DIV ></BODY ></HTML >