# perldoc > Template::Iterator

---
type: CommandReference
command: Template::Iterator
mode: perldoc
section: 3
source: perldoc
---

## Quick Reference

- `Template::Iterator->new(\@data)` — constructor from list or hash
- `$iter->get_first()` — get first item
- `$iter->get_next()` — get next item
- `$iter->size()` — get dataset size
- `$iter->index()` — current index (0..max)
- `$iter->count()` — current count (1..size)
- `$iter->first()` — true if on first iteration
- `$iter->last()` — true if on last iteration

## Name

`Template::Iterator` — Data iterator used by the FOREACH directive

## Synopsis

perl
my $iter = Template::Iterator->new(\@data, \%options);
## Options

- `new($data)` — Constructor. Accepts a list reference, hash reference (expands to `{key=>..., value=>...}` sorted by keys), a single item (auto-wrapped in list), or a blessed ARRAY object. If the object provides `as_list()`, that method is called to get the data list. Returns iterator object.
- `get_first()` — Returns `($value, $error)` for the first item. `$error` is zero/undef on success, `STATUS_DONE` if list is empty.
- `get_next()` — Returns `($value, $error)` for the next item. `STATUS_DONE` if all items visited.
- `get_all()` — Returns `(\@values, $error)` for all remaining items. `STATUS_DONE` if all visited.
- `size()` — Returns the size of the data set, or undef if unknown.
- `max()` — Returns the maximum index (last element index), equivalent to `size() - 1`.
- `index()` — Returns current index in range 0 to max().
- `count()` — Returns current iteration count in range 1 to size() (equivalent to `index() + 1`).
- `first()` — Boolean: true if on first iteration.
- `last()` — Boolean: true if on last iteration.
- `prev()` — Returns previous item or undef if on first item.
- `next()` — Returns next item or undef if on last item.
- `parity()` — Returns string "even" or "odd" based on count (1-based). Useful for zebra striping.
- `odd()` — Boolean: true if count is odd (1,3,5,…).
- `even()` — Boolean: true if count is even (2,4,6,…).

## Examples

**Zebra table using `parity()`:**

perl
<table>
[% FOREACH name IN ['Arthur', 'Ford', 'Trillian'] -%]
  <tr class="[% loop.parity %]">
    <td>[% name %]</td>
  </tr>
[% END %]
</table>
Output:

html
<table>
  <tr class="odd">
    <td>Arthur</td>
  </tr>
  <tr class="even">
    <td>Ford</td>
  </tr>
  <tr class="odd">
    <td>Trillian</td>
  </tr>
</table>
**CSS styling:**

css
tr.odd td { background-color: black; color: white; }
tr.even td { background-color: white; color: black; }
**Hash constructor:**

perl
my $iter = Template::Iterator->new({
    foo => 'Foo Item',
    bar => 'Bar Item',
});
# Equivalent to: [ { key => 'bar', value => 'Bar Item' }, { key => 'foo', value => 'Foo Item' } ]
**Single item folding:**

perl
my $iter = Template::Iterator->new('foo');
# Equivalent to: Template::Iterator->new([ 'foo' ])
**Blessed object with `as_list()`:**

perl
package MyListObject;
sub as_list { my $self = shift; return $self; }
package main;
my $list = MyListObject->new('foo', 'bar');
my $iter = Template::Iterator->new($list);
# Returns two items: 'foo', 'bar'
## See Also

- [Template](https://perldoc.perl.org/Template) — the template processing module