Misha
Misha

Reputation: 190

How do I iterate over an array of Strings with the Meteor Spacebars {{#each}} block?

I have a Mongo.Collection that holds 'Question' objects. Each 'Question' has a property called choices

Questions.insert({
    text: 'What is the capitol of the US?',
    choices: [
    "Washington D.C.",
    "Houston",
    "New York City",
    "Los Angeles"
    ],
    correctChoice: 0,
    date: new Date().toDateString()
});

In my template I have this:

<div class="question">
    <div class="question-content">
        <p>{{text}}</p>
        <ul>
            {{#each choices}}
            //?????????
            {{/each}}
        </ul>
    </div>
</div>

What should I put instead of the question marks in order for popular the unordered list with list items that contain the appropriate choices?

Thank you for reading. Sorry if this is easy. I'm still a little bit of a noob at Meteor. =)

Upvotes: 1

Views: 854

Answers (1)

David Weldon
David Weldon

Reputation: 64312

It looks like this was solved in a comment, but just so this question has an answer: you should use this in cases where the current context is a primitive.

<div class="question">
  <div class="question-content">
    <p>{{text}}</p>
    <ul>
      {{#each choices}}
        <li>{{this}}</li>
      {{/each}}
    </ul>
  </div>
</div>

Upvotes: 9

Related Questions