2

I have this simple array that I pass to the template;

Template.incomeTotals.total = function() {
  var array = [];
  array[0] = [100000, 59, 58900];
  return array;
};

and then this HTML file;

<body>
 <table>
   {{> incomeTotals}}
 </table>
</body>

<template name="incomeTotals">
  {{#each total}}
   <tr>
    <th>{{.0}}</th>
    <th>{{.1}}%</th>
    <th>${{.2}}</th>
   </tr>
  {{/each}}
</template>

What I am trying to do is get these three values all displayed on the same row, but all the values come out on the second row as 100000,59,58900%. I saw in a similar question where someone was using the form of {{#each total.array}} but I got no output when using that syntax.

Update:

Here's what ended up working for anyone else stumbling across this:

Template.incomeTotals.total = function() {
  var array = [];
  array[0] = {budgetTotal:100000, achievedAvg:59, sumTotals:58900};
  return array;
};

HTML;

<body>
 <table>
   {{> incomeTotals}}
 </table>
</body>

<template name="incomeTotals">
  {{#each total}}
   <tr>
    <th>{{budgetTotal}}</th>
    <th>{{achievedAvg}}%</th>
    <th>${{sumTotals}}</th>
   </tr>
  {{/each}}
</template>

2 Answers 2

4

To access arrays by index in Meteor templates:

{{myArray.[0]}}
{{myArray.[0].that}}
Sign up to request clarification or add additional context in comments.

Comments

0

Not an answer yet, but try:

<template name="incomeTotals">
  {{#each total}}
   <tr>
    <th>{{this[0]}}</th>
    <th>{{this[1]}}%</th>
    <th>${{this[2]}}</th>
   </tr>
  {{/each}}
</template>

3 Comments

It doesn't like the bracket notation. Also tried this.1 and it doesn't like that either. I've also tried ` array[0] = {budgetTotal:100000, achievedAvg:59, sumTotals:58900}; ` and then referencing them by name but that doesn't work either.
with {budgetTotal ...} you have to use in template: {{budgetTotal}} did you use that?
Sending a hash in the array and then referencing them by name like you said worked! I had tried earlier to do var array = [{name:value, ...}] and that didn't work using {{budgetTotal ...}} but that didn't work. But creating the empty array and then adding the hash to array[0] is working. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.