0

If I have the following array

array (
    'people' => array(
        [0] => array('name'=>'name1'),
        [1] => array('name'=>'name2', 'last' => true)
    )
);

and I want to output it using a Mustache template so that the final html looks like this:

<div>
    Names: name1, name2
</div>

Though if the array is empty then it shouldn't output anything.

This is my current template

<div>
    {{#people}}
        Names: {{name}}{{^last}}, {{/last}}
    {{/people}}
</div>

which outputs

<div>
    Names: name1, Names: name2
</div>

I understand why it doesn't output what I want but I don't know how to solve it.

Oskar Persson
  • 6,605
  • 15
  • 63
  • 124

1 Answers1

2
<div>
    {{#people.0}}
        Names:
        {{#people}}
            {{name}}{{^last}}, {{/last}}
        {{/people}}
    {{/people.0}}
</div>

The reason you were getting Names: written twice was because everything inside {{#people}} and {{\people}} is treated as a loop.

Oskar Persson
  • 6,605
  • 15
  • 63
  • 124