{section},{sectionelse} Attribute Name Type Required Default Description name string Yes n/a The name of the section loop mixed Yes n/a Value to determine the number of loop iterations start integer No 0 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. step integer No 1 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. max integer No n/a Sets the maximum number of times the section will loop. show boolean No true determines whether or not to show this section Template sections are used for looping over arrays of data (unlike {foreach}). All {section} tags must be paired with {/section} tags. Required parameters are name and loop. The name of the {section} can be anything you like, made up of letters, numbers and underscores. Sections can be nested, and the nested section names must be unique from each other. The loop variable (usually an array of values) determines the number of times the section will loop. When printing a variable within a section, the section name must be given next to variable name within brackets []. {sectionelse} is executed when there are no values in the loop variable. {section} assign('custid',$data); ?> ]]> {/section}
{* print out all the values of the $custid array reversed *} {section name=foo loop=$custid step=-1} {$custid[foo]} {/section} ]]>
The above example will output: id: 1001
id: 1002

id: 1002
id: 1001
id: 1000
]]>
Another couple of examples without an assigned array. {section name=bar loop=21 max=6 step=-2} {$smarty.section.bar.index} {/section} ]]> The above example will output: 20 18 16 14 12 10 ]]>
{section} loop variable assign('custid',$id); $fullnames = array('John Smith','Jack Jones','Jane Munson'); $smarty->assign('name',$fullnames); $addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st'); $smarty->assign('address',$addr); ?> ]]> id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}

{/section} ]]>
The above example will output: id: 1000
name: John Smith
address: 253 N 45th

id: 1001
name: Jack Jones
address: 417 Mulberry ln

id: 1002
name: Jane Munson
address: 5605 apple st

]]>
{section} naming id: {$custid[anything]}
name: {$name[anything]}
address: {$address[anything]}

{/section} ]]>
nested sections assign('custid',$id); $fullnames = array('John Smith','Jack Jones','Jane Munson'); $smarty->assign('name',$fullnames); $addr = array('253 N 45th', '417 Mulberry ln', '5605 apple st'); $smarty->assign('address',$addr); $types = array( array( 'home phone', 'cell phone', 'e-mail'), array( 'home phone', 'web'), array( 'cell phone') ); $smarty->assign('contact_type', $types); $info = array( array('555-555-5555', '666-555-5555', 'john@myexample.com'), array( '123-456-4', 'www.example.com'), array( '0457878') ); $smarty->assign('contact_info', $info); ?> ]]> id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}
{section name=contact loop=$contact_type[customer]} {$contact_type[customer][contact]}: {$contact_info[customer][contact]}
{/section} {/section} ]]>
The above example will output: id: 1000
name: John Smith
address: 253 N 45th
home phone: 555-555-5555
cell phone: 666-555-5555
e-mail: john@myexample.com

id: 1001
name: Jack Jones
address: 417 Mulberry ln
home phone: 123-456-4
web: www.example.com

id: 1002
name: Jane Munson
address: 5605 apple st
cell phone: 0457878
]]>
sections and associative arrays 'John Smith', 'home' => '555-555-5555', 'cell' => '666-555-5555', 'email' => 'john@myexample.com'), array('name' => 'Jack Jones', 'home' => '777-555-5555', 'cell' => '888-555-5555', 'email' => 'jack@myexample.com'), array('name' => 'Jane Munson', 'home' => '000-555-5555', 'cell' => '123456', 'email' => 'jane@myexample.com') ); $smarty->assign('contacts',$data); ?> ]]> name: {$contacts[customer].name}
home: {$contacts[customer].home}
cell: {$contacts[customer].cell}
e-mail: {$contacts[customer].email}

{/section} ]]>
The above example will output: name: John Smith
home: 555-555-5555
cell: 666-555-5555
e-mail: john@myexample.com

name: Jack Jones
home phone: 777-555-5555
cell phone: 888-555-5555
e-mail: jack@myexample.com

name: Jane Munson
home phone: 000-555-5555
cell phone: 123456
e-mail: jane@myexample.com

]]>
Database example (eg using Pear or Adodb) assign('contacts',$db->getAll($sql) ); ?> ]]>  Name>HomeCellEmail {section name=co loop=$contacts} view {$contacts[co].name} {$contacts[co].home} {$contacts[co].cell} {$contacts[co].email} {/section} ]]>
{sectionelse} {sectionelse} there are no values in $custid. {/section} ]]> Sections also have their own variables that handle section properties. These are indicated like so: {$smarty.section.sectionname.varname} As of Smarty 1.5.0, the syntax for section property variables has changed from {%sectionname.varname%} to {$smarty.section.sectionname.varname}. The old syntax is still supported, but you will only see examples of the new syntax. index index is used to display the current array index, starting with zero (or the start attribute if given), and incrementing by one (or by the step attribute if given.) Technical Note If the step and start section properties are not modified, then this works the same as the iteration section property, except it starts at 0 instead of 1. {section} property index {/section} ]]> The above example will output: 1 id: 1001
2 id: 1002
]]>
index_prev index_prev is used to display the previous loop index. on the first loop, this is set to -1. index_next index_next is used to display the next loop index. On the last loop, this is still one more than the current index (respecting the setting of the step attribute, if given.) {section} property index_next and index_prev assign('custid',$data); ?> ]]> indexid index_prevprev_id index_nextnext_id {section name=cus loop=$custid} {$smarty.section.cus.index}{$custid[cus]} {$smarty.section.cus.index_prev}{$custid[cus.index_prev]} {$smarty.section.cus.index_next}{$custid[cus.index_next]} {/section} ]]> The above example will output a table containing the following: iteration iteration is used to display the current loop iteration. This is not affected by the section properties start, step and max, unlike the index property. Iteration also starts with 1 instead of 0 like index. rownum is an alias to iteration, they work identical. {section} property iteration assign('custid',$id); ?> ]]> {/section} ]]> The above example will output: iteration=2 index=7 id=3007
iteration=3 index=9 id=3009
iteration=4 index=11 id=3011
iteration=5 index=13 id=3013
iteration=6 index=15 id=3015
]]>
This example uses the iteration property to output a table header block every five rows (uses {if} with the mod operator). {section name=co loop=$contacts} {if $smarty.section.co.iteration % 5 == 1}  Name>HomeCellEmail {/if}
view {$contacts[co].name} {$contacts[co].home} {$contacts[co].cell} {$contacts[co].email} {/section} ]]> first first is set to true if the current section iteration is the first one. last last is set to true if the current section iteration is the last one. {section} property first and last This example loops the $customers array; outputs a header block on the first iteration and on the last outputs the footer block (uses section total property) idcustomer {/if} {$customers[customer].id}} {$customers[customer].name} {if $smarty.section.customer.last} {$smarty.section.customer.total} customers {/if} {/section} ]]> rownum rownum is used to display the current loop iteration, starting with one. It is an alias to iteration, they work identically. loop loop is used to display the last index number that this section looped. This can be used inside or after the section. {section} property index {/section} There were {$smarty.section.customer.loop} customers shown above. ]]> The above example will output: 1 id: 1001
2 id: 1002
There were 3 customers shown above. ]]>
show show is used as a parameter to section. show is a boolean value, true or false. If false, the section will not be displayed. If there is a {sectionelse} present, that will be alternately displayed. {section} attribute show {/section} {if $smarty.section.customer.show} the section was shown. {else} the section was not shown. {/if} ]]> The above example will output: 2 id: 1001
3 id: 1002
the section was shown. ]]>
total total is used to display the number of iterations that this section will loop. This can be used inside or after the section. {section} property total {/section} There were {$smarty.section.customer.total} customers shown above. ]]> The above example will output: 2 id: 1002
4 id: 1004
There were 3 customers shown above. ]]>
See also {foreach} and $smarty.section.