Search code examples
phptemplatingmustache

Mustache (PHP) Outputting associative array keys


In Mustache can I print out the name of an associative array key instead of its value?

i.e. So instead of this:

$cars= array(
  'name'=>'ferrari', 'color'=>'red', 
  'name'=>'lambo', 'color'=>'yellow'
);
....
{{#cars}}
    {{name}} is {{color}}
{{/cars}}

I would prefer to have a data source with a smaller footprint:

$cars= array('ferrari'=>'red', 'lambo'=>'yellow');
....
{{#cars}}
    {{array_key_here}} is {{.}}
{{/cars}}

Is it possible?


Solution

  • I'm sure the OP has already moved on, but to anyone stumbling upon this post, I'd just like to point out that the reason this is not possible is because there is no predictable means of referencing anything in that array.

    Think of a key in terms of a map, and you have more elaboration.