# {section}, {sectionelse}
A `{section}` is for looping over **sequentially indexed arrays of
data**, unlike [`{foreach}`](./language-function-foreach.md) which is used
to loop over a **single associative array**. Every `{section}` tag must
be paired with a closing `{/section}` tag.
> **Note**
>
> The [`{foreach}`](./language-function-foreach.md) loop can do everything a
> {section} loop can do, and has a simpler and easier syntax. It is
> usually preferred over the {section} loop.
> **Note**
>
> {section} loops cannot loop over associative arrays, they must be
> numerically indexed, and sequential (0,1,2,\...). For associative
> arrays, use the [`{foreach}`](./language-function-foreach.md) loop.
## Attributes
| Attribute Name | Required | Description |
|----------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| name | Yes | The name of the section |
| loop | Yes | Value to determine the number of loop iterations |
| start | No | 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. Defaults to 0. |
| step | No | 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. Defaults to 1. |
| max | No | Sets the maximum number of times the section will loop. |
| show | No | Determines whether to show this section (defaults to true) |
## Option Flags
| Name | Description |
|---------|------------------------------------------|
| nocache | Disables caching of the `{section}` loop |
- Required attributes are `name` and `loop`.
- The `name` of the `{section}` can be anything you like, made up of
letters, numbers and underscores, like [PHP
variables](https://www.php.net/language.variables).
- {section}'s can be nested, and the nested `{section}` names must be
unique from each other.
- The `loop` attribute, usually an array of values, determines the
number of times the `{section}` will loop. You can also pass an
integer as the loop value.
- 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.
- A `{section}` also has its own variables that handle `{section}`
properties. These properties are accessible as:
[`{$smarty.section.name.property}`](../language-variables/language-variables-smarty.md#smartysection-languagevariablessmartyloops)
where "name" is the attribute `name`.
- `{section}` properties are [`index`](#index),
[`index_prev`](#index_prev),
[`index_next`](#index_next),
[`iteration`](#iteration),
[`first`](#first),
[`last`](#last),
[`rownum`](#rownum),
[`loop`](#loop), [`show`](#show),
[`total`](#total).
[`assign()`](../../programmers/api-functions/api-assign.md) an array to Smarty
## Examples
```php
assign('custid', $data);
```
The template that outputs the array
```smarty
{* this example will print out all the values of the $custid array *}
{section name=customer loop=$custid}
{section customer $custid} {* short-hand *}
id: {$custid[customer]}
{/section}
name: {$contacts[customer].name}
home: {$contacts[customer].home}
cell: {$contacts[customer].cell}
e-mail: {$contacts[customer].email}
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
id: {$custid[customer]}
name: {$name[customer]}
address: {$address[customer]}
id: 1000
name: John Smith
address: 253 Abbey road
id: 1001
name: Jack Jones
address: 417 Mulberry ln
id: 1002
name: Jane Munson
address: 5605 apple st
Name> | Home | Cell | ||
---|---|---|---|---|
view | {$contacts[co].name} | {$contacts[co].home} | {$contacts[co].cell} | {$contacts[co].email} |
No items found |
index | id | index_prev | prev_id | index_next | next_id |
---|---|---|---|---|---|
{$smarty.section.row.index} | {$rows[row]} | {$smarty.section.row.index_prev} | {$rows[row.index_prev]} | {$smarty.section.row.index_next} | {$rows[row.index_next]} |
Name> | Home | Cell | ||
---|---|---|---|---|
view | {$contacts[co].name} | {$contacts[co].home} | {$contacts[co].cell} | {$contacts[co].email} |
id | customer |
---|---|
{$customers[customer].id}} | {$customers[customer].name} |
{$smarty.section.customer.total} customers |