It is not possible to iterate over an associative array in Mustache, because Mustache sees it as a "hash" rather than an iterable list. And even if you could iterate over the list, you would not be able to access the keys.
In order to do this, you must prepare your data. You could do it with a foreach loop before you pass the data into Mustache, or you could do it by wrapping your array in a "Presenter". Something like this ought to do the trick:
<?php
class IteratorPresenter implements IteratorAggregate
{
private $values;
public function __construct($values)
{
if (!is_array($values) && !$values instanceof Traversable) {
throw new InvalidArgumentException('IteratorPresenter requires an array or Traversable object');
}
$this->values = $values;
}
public function getIterator()
{
$values = array();
foreach ($this->values as $key => $val) {
$values[$key] = array(
'key' => $key,
'value' => $val,
'first' => false,
'last' => false,
);
}
$keys = array_keys($values);
if (!empty($keys)) {
$values[reset($keys)]['first'] = true;
$values[end($keys)]['last'] = true;
}
return new ArrayIterator($values);
}
}
Then simply wrap your array in the Presenter:
$view['data'] = new IteratorPresenter($view['data']);
You now have access to the keys and values while iterating over your data:
{{# data }}
{{ key }}: {{ value }}
{{/ data }}