| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | # {foreach},{foreachelse}
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `{foreach}` is used for looping over arrays of data. `{foreach}` has a | 
					
						
							|  |  |  | simpler and cleaner syntax than the | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | [`{section}`](./language-function-section.md) loop, and can also loop over | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | associative arrays. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## Option Flags
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | | Name    | Description                              | | 
					
						
							|  |  |  | |---------|------------------------------------------| | 
					
						
							|  |  |  | | nocache | Disables caching of the `{foreach}` loop | | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | ## Examples
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ```smarty | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {foreach $arrayvar as $itemvar} | 
					
						
							|  |  |  |   {$itemvar|escape} | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {foreach $arrayvar as $keyvar=>$itemvar} | 
					
						
							|  |  |  |   {$keyvar}: {$itemvar|escape} | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | > **Note**
 | 
					
						
							|  |  |  | > | 
					
						
							|  |  |  | > This foreach syntax does not accept any named attributes. This syntax
 | 
					
						
							|  |  |  | > is new to Smarty 3, however the Smarty 2.x syntax
 | 
					
						
							|  |  |  | > `{foreach from=$myarray key="mykey" item="myitem"}` is still
 | 
					
						
							|  |  |  | > supported.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   `{foreach}` loops can be nested. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   The `array` variable, usually an array of values, determines the | 
					
						
							|  |  |  |     number of times `{foreach}` will loop. You can also pass an integer | 
					
						
							|  |  |  |     for arbitrary loops. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | -   `{foreachelse}` is executed when there are no values in the `array` | 
					
						
							|  |  |  |     variable. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | -   `{foreach}` properties are [`@index`](#index), | 
					
						
							|  |  |  |     [`@iteration`](#iteration), | 
					
						
							|  |  |  |     [`@first`](#first), | 
					
						
							|  |  |  |     [`@last`](#last), | 
					
						
							|  |  |  |     [`@show`](#show), | 
					
						
							|  |  |  |     [`@total`](#total). | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | -   `{foreach}` constructs are [`{break}`](#break), | 
					
						
							|  |  |  |     [`{continue}`](#continue). | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | -   Instead of specifying the `key` variable you can access the current | 
					
						
							|  |  |  |     key of the loop item by `{$item@key}` (see examples below). | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | > **Note**
 | 
					
						
							|  |  |  | > | 
					
						
							|  |  |  | > The `$var@property` syntax is new to Smarty 3, however when using the
 | 
					
						
							|  |  |  | > Smarty 2 `{foreach from=$myarray key="mykey" item="myitem"}` style
 | 
					
						
							|  |  |  | > syntax, the `$smarty.foreach.name.property` syntax is still supported.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | > **Note**
 | 
					
						
							|  |  |  | > | 
					
						
							|  |  |  | > Although you can retrieve the array key with the syntax
 | 
					
						
							|  |  |  | > `{foreach $myArray as $myKey => $myValue}`, the key is always
 | 
					
						
							|  |  |  | > available as `$myValue@key` within the foreach loop.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```php | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | $arr = array('red', 'green', 'blue'); | 
					
						
							|  |  |  | $smarty->assign('myColors', $arr); | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | Template to output `$myColors` in an un-ordered list | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | <ul> | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |     {foreach $myColors as $color} | 
					
						
							|  |  |  |         <li>{$color}</li> | 
					
						
							|  |  |  |     {/foreach} | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | </ul> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							|  |  |  | The above example will output: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | <ul> | 
					
						
							|  |  |  |     <li>red</li> | 
					
						
							|  |  |  |     <li>green</li> | 
					
						
							|  |  |  |     <li>blue</li> | 
					
						
							|  |  |  | </ul> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```php       | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  | $people = array('fname' => 'John', 'lname' => 'Doe', 'email' => 'j.doe@example.com'); | 
					
						
							|  |  |  | $smarty->assign('myPeople', $people); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | Template to output `$myArray` as key/value pairs. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | <ul> | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |     {foreach $myPeople as $value} | 
					
						
							|  |  |  |        <li>{$value@key}: {$value}</li> | 
					
						
							|  |  |  |     {/foreach} | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | </ul> | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  |       | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | The above example will output: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | <ul> | 
					
						
							|  |  |  |     <li>fname: John</li> | 
					
						
							|  |  |  |     <li>lname: Doe</li> | 
					
						
							|  |  |  |     <li>email: j.doe@example.com</li> | 
					
						
							|  |  |  | </ul> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							|  |  |  | Assign an array to Smarty, the key contains the key for each looped | 
					
						
							|  |  |  | value. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```php | 
					
						
							|  |  |  | <?php | 
					
						
							|  |  |  |  $smarty->assign( | 
					
						
							|  |  |  |     'contacts',  | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |          ['phone' => '555-555-1234', 'fax' => '555-555-5678', 'cell' => '555-555-0357'], | 
					
						
							|  |  |  |          ['phone' => '800-555-4444', 'fax' => '800-555-3333', 'cell' => '800-555-2222'], | 
					
						
							|  |  |  |      ] | 
					
						
							|  |  |  |  ); | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | The template to output `$contact`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {* key always available as a property *} | 
					
						
							|  |  |  | {foreach $contacts as $contact} | 
					
						
							|  |  |  |   {foreach $contact as $value} | 
					
						
							|  |  |  |     {$value@key}: {$value} | 
					
						
							|  |  |  |   {/foreach} | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | {* accessing key the PHP syntax alternate *} | 
					
						
							|  |  |  | {foreach $contacts as $contact} | 
					
						
							|  |  |  |   {foreach $contact as $key => $value} | 
					
						
							|  |  |  |     {$key}: {$value} | 
					
						
							|  |  |  |   {/foreach} | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							|  |  |  | Either of the above examples will output: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ``` | 
					
						
							|  |  |  |   phone: 555-555-1234 | 
					
						
							|  |  |  |   fax: 555-555-5678 | 
					
						
							|  |  |  |   cell: 555-555-0357 | 
					
						
							|  |  |  |   phone: 800-555-4444 | 
					
						
							|  |  |  |   fax: 800-555-3333 | 
					
						
							|  |  |  |   cell: 800-555-2222 | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							|  |  |  | A database (PDO) example of looping over search results. This example is | 
					
						
							|  |  |  | looping over a PHP iterator instead of an array(). | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```php | 
					
						
							|  |  |  | <?php  | 
					
						
							| 
									
										
										
										
											2023-02-06 15:18:47 +01:00
										 |  |  |   use Smarty\Smarty; | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  |   $smarty = new Smarty;  | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  |   $dsn = 'mysql:host=localhost;dbname=test';  | 
					
						
							|  |  |  |   $login = 'test';  | 
					
						
							|  |  |  |   $passwd = 'test';  | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  |   // setting PDO to use buffered queries in mysql is  | 
					
						
							|  |  |  |   // important if you plan on using multiple result cursors  | 
					
						
							|  |  |  |   // in the template.  | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  |   $db = new PDO($dsn, $login, $passwd, array(  | 
					
						
							|  |  |  |      PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));  | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  |   $res = $db->prepare("select * from users");  | 
					
						
							|  |  |  |   $res->execute();  | 
					
						
							|  |  |  |   $res->setFetchMode(PDO::FETCH_LAZY);  | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  |   // assign to smarty  | 
					
						
							|  |  |  |   $smarty->assign('res',$res);  | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  |   $smarty->display('index.tpl');?> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {foreach $res as $r}  | 
					
						
							|  |  |  |   {$r.id}  | 
					
						
							|  |  |  |   {$r.name} | 
					
						
							|  |  |  | {foreachelse} | 
					
						
							|  |  |  |   .. no results ..  | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | The above is assuming the results contain the columns named `id` and | 
					
						
							|  |  |  | `name`. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | What is the advantage of an iterator vs. looping over a plain old array? | 
					
						
							|  |  |  | With an array, all the results are accumulated into memory before being | 
					
						
							|  |  |  | looped. With an iterator, each result is loaded/released within the | 
					
						
							|  |  |  | loop. This saves processing time and memory, especially for very large | 
					
						
							|  |  |  | result sets. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## @index
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `index` contains the current array index, starting with zero. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {* output empty row on the 4th iteration (when index is 3) *} | 
					
						
							|  |  |  | <table> | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |     {foreach $items as $i} | 
					
						
							|  |  |  |       {if $i@index eq 3} | 
					
						
							|  |  |  |          {* put empty table row *} | 
					
						
							|  |  |  |          <tr><td>nbsp;</td></tr> | 
					
						
							|  |  |  |       {/if} | 
					
						
							|  |  |  |       <tr><td>{$i.label}</td></tr> | 
					
						
							|  |  |  |     {/foreach} | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | </table> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## @iteration
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `iteration` contains the current loop iteration and always starts at | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | one, unlike [`index`](#index). It is incremented by one | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | on each iteration. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | The *"is div by"* operator can be used to detect a specific iteration. | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | Here we bold-face the name every 4th iteration. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {foreach $myNames as $name} | 
					
						
							|  |  |  |   {if $name@iteration is div by 4} | 
					
						
							|  |  |  |     <b>{$name}</b> | 
					
						
							|  |  |  |   {/if} | 
					
						
							|  |  |  |   {$name} | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | The *"is even by"* and *"is odd by"* operators can be used to | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | alternate something every so many iterations. Choosing between even or | 
					
						
							|  |  |  | odd rotates which one starts. Here we switch the font color every 3rd | 
					
						
							|  |  |  | iteration. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  |  {foreach $myNames as $name} | 
					
						
							|  |  |  |    {if $name@iteration is even by 3} | 
					
						
							|  |  |  |      <span style="color: #000">{$name}</span> | 
					
						
							|  |  |  |    {else} | 
					
						
							|  |  |  |      <span style="color: #eee">{$name}</span> | 
					
						
							|  |  |  |    {/if} | 
					
						
							|  |  |  |  {/foreach} | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | This will output something similar to this: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```html | 
					
						
							|  |  |  | <span style="color: #000">...</span> | 
					
						
							|  |  |  | <span style="color: #000">...</span> | 
					
						
							|  |  |  | <span style="color: #000">...</span> | 
					
						
							|  |  |  | <span style="color: #eee">...</span> | 
					
						
							|  |  |  | <span style="color: #eee">...</span> | 
					
						
							|  |  |  | <span style="color: #eee">...</span> | 
					
						
							|  |  |  | <span style="color: #000">...</span> | 
					
						
							|  |  |  | <span style="color: #000">...</span> | 
					
						
							|  |  |  | <span style="color: #000">...</span> | 
					
						
							|  |  |  | <span style="color: #eee">...</span> | 
					
						
							|  |  |  | <span style="color: #eee">...</span> | 
					
						
							|  |  |  | <span style="color: #eee">...</span> | 
					
						
							|  |  |  | ... | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |         | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## @first
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `first` is TRUE if the current `{foreach}` iteration is the initial one. | 
					
						
							|  |  |  | Here we display a table header row on the first iteration. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {* show table header at first iteration *} | 
					
						
							|  |  |  | <table> | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |     {foreach $items as $i} | 
					
						
							|  |  |  |       {if $i@first} | 
					
						
							|  |  |  |         <tr> | 
					
						
							|  |  |  |           <th>key</td> | 
					
						
							|  |  |  |           <th>name</td> | 
					
						
							|  |  |  |         </tr> | 
					
						
							|  |  |  |       {/if} | 
					
						
							|  |  |  |       <tr> | 
					
						
							|  |  |  |         <td>{$i@key}</td> | 
					
						
							|  |  |  |         <td>{$i.name}</td> | 
					
						
							|  |  |  |       </tr> | 
					
						
							|  |  |  |     {/foreach} | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | </table> | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## @last
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `last` is set to TRUE if the current `{foreach}` iteration is the final | 
					
						
							|  |  |  | one. Here we display a horizontal rule on the last iteration. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {* Add horizontal rule at end of list *} | 
					
						
							|  |  |  | {foreach $items as $item} | 
					
						
							|  |  |  |   <a href="#{$item.id}">{$item.name}</a>{if $item@last}<hr>{else},{/if} | 
					
						
							|  |  |  | {foreachelse} | 
					
						
							|  |  |  |   ... no items to loop ... | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |        | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## @show
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | The show `show` property can be used after the execution of a | 
					
						
							|  |  |  | `{foreach}` loop to detect if data has been displayed or not. `show` is | 
					
						
							|  |  |  | a boolean value. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | <ul> | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |     {foreach $myArray as $name} | 
					
						
							|  |  |  |         <li>{$name}</li> | 
					
						
							|  |  |  |     {/foreach} | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | </ul> | 
					
						
							|  |  |  | {if $name@show} do something here if the array contained data {/if} | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## @total
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `total` contains the number of iterations that this `{foreach}` will | 
					
						
							|  |  |  | loop. This can be used inside or after the `{foreach}`. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  | {* show number of rows at end *} | 
					
						
							|  |  |  | {foreach $items as $item} | 
					
						
							|  |  |  |   {$item.name}<hr/> | 
					
						
							|  |  |  |   {if $item@last} | 
					
						
							|  |  |  |     <div id="total">{$item@total} items</div> | 
					
						
							|  |  |  |   {/if} | 
					
						
							|  |  |  | {foreachelse} | 
					
						
							|  |  |  |  ... no items to loop ... | 
					
						
							|  |  |  | {/foreach} | 
					
						
							|  |  |  | ``` | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | See also [`{section}`](./language-function-section.md), | 
					
						
							|  |  |  | [`{for}`](./language-function-for.md) and | 
					
						
							|  |  |  | [`{while}`](./language-function-while.md) | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## {break}
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `{break}` aborts the iteration of the array | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty      | 
					
						
							|  |  |  |   {$data = [1,2,3,4,5]} | 
					
						
							|  |  |  |   {foreach $data as $value} | 
					
						
							|  |  |  |     {if $value == 3} | 
					
						
							|  |  |  |       {* abort iterating the array *} | 
					
						
							|  |  |  |       {break} | 
					
						
							|  |  |  |     {/if} | 
					
						
							|  |  |  |     {$value} | 
					
						
							|  |  |  |   {/foreach} | 
					
						
							|  |  |  |   {* | 
					
						
							|  |  |  |     prints: 1 2 | 
					
						
							|  |  |  |   *} | 
					
						
							|  |  |  | ```  | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  |         | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ## {continue}
 | 
					
						
							| 
									
										
										
										
											2021-12-03 11:59:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | `{continue}` leaves the current iteration and begins with the next | 
					
						
							|  |  |  | iteration. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-06 10:40:00 +01:00
										 |  |  | ```smarty | 
					
						
							|  |  |  |   {$data = [1,2,3,4,5]} | 
					
						
							|  |  |  |   {foreach $data as $value} | 
					
						
							|  |  |  |     {if $value == 3} | 
					
						
							|  |  |  |       {* skip this iteration *} | 
					
						
							|  |  |  |       {continue} | 
					
						
							|  |  |  |     {/if} | 
					
						
							|  |  |  |     {$value} | 
					
						
							|  |  |  |   {/foreach} | 
					
						
							|  |  |  |   {* | 
					
						
							|  |  |  |     prints: 1 2 4 5 | 
					
						
							|  |  |  |   *} | 
					
						
							|  |  |  | ``` |