Home  ›  ClipBucket v2  ›  Developers Guide  ›  Template Functions  ›  {section } Loop

Tags: , , ,

{section } Loop

{section } is used to loop through an array and display the result one by one. For starters, we did not create {section } function, it is Smarty function, just like while for php.

Usage
{section name=loop_name loop=$array}
	{$array[loop_name].column_name}
{/section}

Let’s break it down. First

{section name=loop_name loop=$array}

We give our section a name, this can be anything you want, like v_list or videoList. Next we provide section the array we going to loop through. Next part is to display the information you want.

{$array[loop_name].column_name}

To display any type of information, we need follow a required syntax. First we write down the variable in which array is stored, in our case $array, then loop_name. Third is mysql database column name. Let’s say we need want to display video title. We will be using following code. List of mysql Database Column Names.

{$video_array[v_list].title}

Below is list of attributes you can use in {section }

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.

How to use {sectionelse}

{sectionelse} is used when provided array is empty. For Example,

{section name=loop_name loop=$array}
	{$array[loop_name].column_name}
{sectionelse}
	Do something here if $array is empty
{/section}

If {section} finds that variable $array is empty, then {sectionelse} is executed. Think of it as PHP if & else conditions.

{section} Variables

A {section} also has its own variables that handle {section} properties. These properties are accessible as:
{$smarty.section.name.property} where “name” is the attribute name. {section} properties are index, iteration, first, last, rownum, total.

.index

index contains the current array index, starting with zero or the start attribute if given. It increments by one or by the step attribute if given.

If the step and start properties are not modified, then this works the same as the iteration property, except it starts at zero instead of one.
Example
{section name=loop_name loop=$array}
	Index of $array is : {$smarty.section.loop_name.index}<br/>
{/section}

Above code will print following,

Index of $array is : 0<br/>
Index of $array is : 1<br/>
Index of $array is : 2<br/>
Index of $array is : 3<br/>
.iteration

iteration contains the current loop iteration and starts at one.

This is not affected by the {section} properties start, step and max, unlike the index property. iteration also starts with one instead of zero unlike index. rownum is an alias to iteration, they are identical.
Example
{section name=loop_name loop=$array}
	$array Iteration is : {$smarty.section.loop_name.iteration}<br/>
{/section}

This will print following:

$array Iteration is : 1<br/>
$array Iteration is : 2<br/>
$array Iteration is : 3<br/>
$array Iteration is : 4<br/>
.first

first is set to TRUE if the current {section} iteration is the initial one.

.last

last is set to TRUE if the current {section} iteration is the final one.

Examples of both .first and .last
{section name=loop_name loop=$array}
    {if $smarty.section.loop_name.first}(This is First) {/if}
    {if $smarty.section.loop_name.last}(This is Last) {/if}
    $array ID is : {$array[loop_name].id}<br/>
{/section}

Above will print following:

(This is First) $array ID is : 25<br/>
$array ID is : 26<br/>
$array ID is : 27<br/>
(This is Last) $array ID is : 28<br/>
.rownum

rownum contains the current loop iteration, starting with one. It is an alias to iteration, they work identically.

.total

total contains the number of iterations that this {section} will loop. This can be used inside or after a {section}.

Example
{section name=loop_name loop=$array}
	loop id is: {$array[loop_name].id}<br/>
{/section}
Number of iterations: {$smarty.section.loop_name.total}

Like mentioned earlier, you can use .total outside {section}. Above code will print following

loop id is: 25 <br/>
loop id is: 26 <br/>
loop id is: 27 <br/>
loop id is: 28 <br/>
Number of iteration: 4


Comments are closed.

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading ... Loading ...
Help us improve the wiki Send Your Comments